私は、誰かがファイルから情報を読み取り、最も一般的に使用されている単語を整理し、各単語が使用された回数を返すことに成功したという投稿を見つけました。入力はコマンドライン引数からのものでしたが、同じスクリプトを実行してから、ファイル名を入力としてスクリプトで実行したいと考えています。私が間違っていることを見つけることができません。
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>