我有两个文件,我想逐行阅读(第一个包含每行一个单词,第二个每行一个句子)。逐行读取文件
目标是计算句子的数量从file 2
包含一个单词在file 1
。
这里是我的代码:
open(my $words, '<:utf8', 'test') or die "Unable to open for read: $!"; `#test file is the file that contain my words`
open(my $sentences, '<:utf8', 'sentences') or die "Unable to open for read: $!"; `#sentences fila that contain one sentence per line`
open my $fh_resultat, ">:utf8", 'result';
my $word;
#i want to calculate the number of sentences from my $sentences that containe word from my file $words
while(defined($word = <$words>)) {
chomp $word ;
$word =~ s/^\s*|\s*$//g;
my $nb = 0;
my $idf;
my $ph;
while (defined ($ph = <$sentences>)){
my @tab = split(/ /, $ph);
chomp @tab ;
foreach my $val(@tab) {
if($word eq $val){
$nb = $nb + 1;
last;
}
}
}
print $fh_resultat "$word:$nb\n";
}
,但只对第一个文件的第一个字的处理!
如果您要求大量的人阅读并理解您的代码,那么尽可能让它阅读起来很简单。我已经做了一些轻量级的重新格式化,以添加一些缩进,并使您对空白的使用更加均匀。请在将来自己做。 –