in reply to Printing keys from a hash by value?
That's a pretty unusual way to use reverse. You'll get away with it as long as your values are all unique, but if any two values are the same, one will get clobbered. Also, since you swapped the keys and values that way before doing your sort, your sort is actually sorting by your words, which used to be your keys and are now your values.
So in my opinion, reversing the hash, even if you know your values are unique, is confusing and unnecessary. Just sort your original hash by the values, and stop after the 20th one.
my %top_words; $top_words{chr(64+$_)x4} = $_ for (1..26); # fill hash with some word/ +number pairs my $counter = 1; for my $word (sort {$top_words{$b} <=> $top_words{$a}} keys %top_words +){ print "$word\n"; last if $counter++ == 20; }
Aaron B.
My Woefully Neglected Blog, where I occasionally mention Perl.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Printing keys from a hash by value?
by ricDeez (Scribe) on Jan 30, 2012 at 16:47 UTC | |
by aaron_baugher (Curate) on Jan 30, 2012 at 20:28 UTC | |
|
Re^2: Printing keys from a hash by value?
by jms53 (Monk) on Jan 30, 2012 at 16:38 UTC | |
by aaron_baugher (Curate) on Jan 30, 2012 at 20:53 UTC | |
by Anonymous Monk on Jan 30, 2012 at 19:49 UTC |