in reply to Using map function to print few elements of list returned by sort function
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Using map function to print few elements of list returned by sort function
by jaypal (Beadle) on May 25, 2014 at 14:55 UTC | |
|
Re^2: Using map function to print few elements of list returned by sort function
by jaypal (Beadle) on May 25, 2014 at 22:24 UTC | |
by LanX (Saint) on May 25, 2014 at 23:02 UTC | |
by jaypal (Beadle) on May 26, 2014 at 00:28 UTC | |
by LanX (Saint) on May 26, 2014 at 11:24 UTC | |
by jaypal (Beadle) on May 26, 2014 at 15:16 UTC | |
by jaypal (Beadle) on May 25, 2014 at 23:33 UTC |