Howdy, I have the following to sort a hash of URLs based on domain/host. It works well for hosts w/ names but may not work great for IP addresses (verify yourself). Assuming %url contains URLS as the keys & anything else for values (i have page titles from ~/.netscape/history.dat, thanks to Netscape::History)...
printf "%s\n\n" , $_ foreach # extract the sorted complete URL list map { $_->[0] } # sort on massged host name or the complete url sort { $a->[1] cmp $b->[1] || $a->[0] cmp $b->[0] } # set up for extracting the complete URL & sortkeys map { # extract host/domain components in reverse order my @host = reverse split '\.' , ( split '/' , substr($_ , index($_, '://') + 3) )[0 +]; [ # current URL $_ , # put domain at front; everything else afterwords # ---- # poo.koo -> poo.koo # web.poo.koo:80 -> poo.koo:80.www # rand.web.poo.koo -> poo.koo.web.rand # ---- (scalar @host > 1 ? $host[1] . '.' : '') . $host[0] . ( scalar @host < 3 ? '' : '.' . join('.' , @host[2..$# +host]) ) ] } keys %url;
...my question is could the domain/host sortkeys be generated by better/less verbose way?
In reply to Sorting URLs on domain/host: sortkeys generation by parv
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |