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 | |
by ikegami (Patriarch) on Jul 19, 2005 at 14:05 UTC |