in reply to Tutelage, part two
{ 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
|
|---|