You don't need a full sort, all you want is to put the
domains in the proper buckets. Here's my solution:
#!/usr/bin/perl
use strict;
use warnings 'all';
my @regexes = map {qr /\.$_$/}
qr {co\.uk},
qr {com}, # No need for the [\w\-] prefix.
qr {pl},
# qr {uk\.com}, # This one already gets grabbed by \.com
;
my @domains = qw {
foo.com
weirdext.za
bar.uk.com
blah.co.uk
perl.pl
zzzz.co.uk
};
my @buckets = map {[]} @regexes, 1;
DOMAIN:
foreach my $domain (@domains) {
for (my $i = 0; $i < @regexes; $i ++) {
next unless $domain =~ qr /$regexes[$i]/;
push @{$buckets [$i]} => $domain;
next DOMAIN;
}
push @{$buckets [-1]} => $domain;
}
print "$_\n" for map {@$_} @buckets;
__END__
blah.co.uk
zzzz.co.uk
foo.com
bar.uk.com
perl.pl
weirdext.za
Abigail