Created
January 18, 2017 14:39
-
-
Save bigpresh/44546a64ba90d69e0589dff7ce0b310f to your computer and use it in GitHub Desktop.
summarise-hostnames
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub summarise_hostnames { | |
my @hostnames = @_; | |
for my $entry (@hostnames) { | |
say "Consider $entry"; | |
my ($leaf, $domain) = split /\./, $entry, 2; | |
if ($domain =~ /\./) { | |
say "OK, add $leaf to $domain list"; | |
push @{ $leaf_by_domain{$domain} }, $leaf; | |
} else { | |
say "Treat $entry as a domain only"; | |
push @domains, $entry; | |
} | |
} | |
return join ', ', | |
( | |
map { | |
@{$leaf_by_domain{$_}} == 1 | |
? $leaf_by_domain{$_}[0].'.'.$_ | |
: "{" . | |
join(',', sort @{ $leaf_by_domain{$_} }) . | |
"}." . $_ | |
} sort keys %leaf_by_domain | |
), | |
@domains; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment