in reply to perl sort issue while going through article at perl.com
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}
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: perl sort issue while going through article at perl.com
by convenientstore (Pilgrim) on Oct 22, 2007 at 05:18 UTC | |
by shmem (Chancellor) on Oct 22, 2007 at 08:16 UTC | |
by convenientstore (Pilgrim) on Oct 22, 2007 at 14:55 UTC | |
by convenientstore (Pilgrim) on Oct 23, 2007 at 00:30 UTC | |
by shmem (Chancellor) on Oct 23, 2007 at 05:51 UTC | |
by convenientstore (Pilgrim) on Oct 23, 2007 at 22:14 UTC | |
by FunkyMonk (Bishop) on Oct 23, 2007 at 22:57 UTC | |
|