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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.