In place of your last code paragraph (starting with foreach $key...), try this:

{ local $, = "\n"; print( ( sort { $hash{$b} <=> $hash{$a} } keys %hash )[0..9] ); }

...if that's too funky for ya-all, you could just do this:

foreach my $key ( (sort {$hash{$b} <=> $hash{$a} } keys %hash )[0..9] +) { print $key, "\n"; }

Both of the preceeding methods take a slice of a list. This assumes that there actually ARE ten words in the list. None of the methods mentioned try to do anything about the possibility of there being more than one word that appears N number of times. The order, in such a case, is undefined. If you want to define it, change the sort to look like this:

sort { $hash{$b} <=> $hash{$a} or $a cmp $b } keys $hash

This will sort by frequency first, and alphabetically second, by short-circuiting with the 'or' to the alphabetic comparison when the frequencies are equal.

Also, this wasn't mentioned in response to your previous "Tutlage" question, but it's looked on favorably in the Monastery to use node titles that describe the question, not the nature of the request for help or level of knowledge of the requestor. Titles like "Newbie help", "Need help", or "Tutelage" don't give anyone any idea what it is they're going to be reading (or choosing not to read, whichever the case may be). ...just a friendly tip. ;)


Dave


In reply to Re: Tutelage, part two by davido
in thread Tutelage, part two by ctp

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.