我在这里发现了一篇文章,有人设法从文件中读取信息并整理出最常用的单词并返回每个单词的使用次数。输入来自命令行参数,但我想执行相同的脚本,然后将文件名作为输入在脚本中运行。我找不到我做错了什么。
print "Type the name of the file: ";
chomp(my $file = <>);
open (FILE, "$file") or die;
while (<FILE>){
$seen{$_}++ for split /\W+/;
}
my $count = 0;
for (sort {
$seen{$b} <=> $seen{$a}
||
lc($a) cmp lc($b)
||
$a cmp $b
} keys %seen)
{
next unless /\w/;
printf "%-20s %5d\n", $seen{$_}, $_;
last if ++$count > 100;
}
close (FILE);
我目前的结果是:
15 0
15 0
10 0
10 0
10 0
5 1
5 0
5 0
5 0
5 0
我想要的结果是:
<word> <number of occurances>
<word> <number of occurances>
<word> <number of occurances>
<word> <number of occurances>
<word> <number of occurances>
<word> <number of occurances>