求助!一道perl语言作业题!在线等!
给定语料文件corpus.txt,编制Perl程序,以空格为隔离,统计文件中出现的单词,输出出现次数大于10次的单词。
[解决办法]
#!/usr/bin/env perluse strict;use warnings;my %fq;open my $fh, '<', 'corpus.txt' or die $!;while (<$fh>) { my @words = split /\s+/; for my $word (@words) { $fq{$word} ? $fq{$word}++ : ($fq{$word} = 1); }}close $fh or die $!;while (my ($k, $v) = each %fq) { print "$k => $v\n" if ($v > 10);}
[解决办法]
#!/usr/bin/perl
open(Out,">result.txt");
open(In,"corpus.txt");
while(<In>){
map {++$hash{$_}} (split);
}
close In;
foreach $word (sort keys(%hash)){
if ($hash{$word} > 10){
print Out "$word: $hash{$word}\n";
}
}
close Out;