my @popular = (sort { $histogram{$b} <=> $histogram{$a} } @unique)[0. +.2]; print "@popular\n"; __END__ tomato watermelon oranges
Correct - hash keys, sorted descending by value, first three ( values are 4,3,2).
my @popular = (sort { $histogram{$a} <=> $histogram{$b} } @unique)[0. +.2]; print "@popular\n"; __END__ apple oranges watermelon
Also correct - hash keys, sorted ascending by value, first three (values are 1,2,3).
If you iterate over the hash keys and print them out with their value, they will come out in any order. A hash isn't sorted.
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
In reply to Re: perl sort issue while going through article at perl.com
by shmem
in thread perl sort issue while going through article at perl.com
by convenientstore
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |