Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Top five words by occurrence

by ikegami (Pope)
on Jul 18, 2005 at 16:45 UTC ( #475807=note: print w/replies, xml ) Need Help??


in reply to Top five words by occurrence

how should I sort this? Then how can I take only the top 5?
my $top = 5; foreach $w (sort { $count{$b} <=> $count{$a} } keys %count) { last if $top--; print "$count{$w} $w\n"; }

or

my $top = 5; foreach $w ( (sort { $count{$b} <=> $count{$a} } keys %count)[0..$top-1] ) { print "$count{$w} $w\n"; }

If you need to handle ties, the following lists more than 5 if there are ties for the 5th spot:

my $top = 5; my $last_count = -1; foreach $w (sort { $count{$b} <=> $count{$a} } keys %count) { last if $top == 1 and $last_count != $count{$w}; $top--; $last_count = $count{$w}; print "$count{$w} $w\n"; }

I'll let someone else help you to extract words.

Replies are listed 'Best First'.
Re^2: Top five words by occurrence
by Dietz (Curate) on Jul 19, 2005 at 10:29 UTC
    last if $top--;
    In your first example this would evaluate to 'last if true', thus exiting the loop before the first print statement would get called.
    Shouldn't that be last unless $top--; or am I overlooking something?
      aye, you're right.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://475807]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2020-02-17 07:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (70 votes). Check out past polls.

    Notices?