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.