While I agree with boftx that a real script would be more appropriate for something like this in a production setting, I appreciate that pushing one-liners to their limits can make for good learning exercises.
As for your splice based solution, I think that's actually pretty good. Here are two small suggestions to tweak it further:
( ZM => [ [20470, "20470:ZM:Samfya:Africa"], [20149, "20149:ZM:Sesheke:Africa"], [18638, "18638:ZM:Siavonga:Africa"] ], ZW => [ ... ], ... )
Here's the one-liner with those changes, in a "scriptified" representation (which I find easier to work with; it's trivial to convert it back to the one-liner format by removing the lines with comments after them):
use warnings; # just for debugging use strict; # just for debugging my (%h, $k, @F); # just for debugging while (<>) { # -n chomp; # -l $\ = "\n"; # -l @F = split(':'); # -F":" -a push @{$h{ $F[1] }}, [$F[0], $_]; } for $k (sort keys %h) { print $_->[1] for splice [sort {$b->[0]<=>$a->[0]} @{$h{$k}}], 0, 4 } # -n
In reply to Re: Using map function to print few elements of list returned by sort function
by smls
in thread Using map function to print few elements of list returned by sort function
by jaypal
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |