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.


In reply to Re: Printing keys from a hash by value? by aaron_baugher
in thread Printing keys from a hash by value? by jms53

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.