请教:统计文本单词数

请教:统计文本单词数

请教:统计文本单词数
处理某文本,做到此步,现需统计文本单词数,每行1单词,我用forfor处理没有搞定,请哪位前辈指点一下啊
示例:
you
will
be
in
my
heart
heart
forever
forever
forever
期望结果(按count降序排列):
word count percent
forever 3 3 %
heart 2 2%
you 1 1%
will 1 1%
be 1 1%
in 1 1%
my 1 1%
可能会有点麻烦...
可能会有点麻烦:

my (%w,$i);
open(F,'words.file') || die "Can't open flie: $!";
my @r = <F>;
chomp @r;
for ( @r ) {
$i++ if ++$w{"$_"};
}
print "Words\tCounts\tpercent\n";
my @e = sort{ $w{$b} <=>$w{$a} } keys %w ;
for ( @e ) {
print "$_\t$w{$_}\t",$w{$_} / $i*100,"%\n"
}




   

[quote]for ( @...
[quote]for ( @r ) {
$i++ if ++$w{"$_"};
}
[/quote]
多谢subs大侠,又学到一招!
[CCB]2[/CCB]
分享:(源于chinaunix.net pupilzeng侠客)

#!/usr/bin/perl -w
use strict;
my %words=();
my $total = 0;
while(<>){
$words{$1}++,$total++ if(/^\s*(\w+)/);
}

my @keys =sort {$words{$b} <=> $words{$a}} keys %words;

print "word\tcount\tpercent\n";
foreach(@keys){
printf "%s\t$words{$_}\t%d%%\n", $_,$words{$_}/$total*100;
}