convenientstore has asked for the wisdom of the Perl Monks concerning the following question:
What is the best way to sort the hash by value and printing out the top 5 results?
For array, I guess you can do something like
is it just my grammar mistake?
But not sure about hash as below method does not work.@count_s = sort { $reportn{$a} <=> $reportn{$b}; } @count_s; foreach (@count_s[0..4]) { print "$_ ====> $reportn{$_}\n"; }
is it just my grammar mistake?
foreach my $hero ( sort keys %total_o) { if ($reportn_o{$hero}) { @reportnL = sort { $total_o{$a} <=> $total_o{$b} } values (%t +otal_o{$hero}); } } for (@reportnL[0..4]) { print "$_\n"; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: sort hash by value
by salva (Canon) on Dec 02, 2007 at 19:55 UTC | |
Re: sort hash by value
by moritz (Cardinal) on Dec 02, 2007 at 18:01 UTC | |
Re: sort hash by value
by grep (Monsignor) on Dec 02, 2007 at 18:11 UTC | |
Re: sort hash by value
by johngg (Canon) on Dec 02, 2007 at 18:44 UTC |
Back to
Seekers of Perl Wisdom