我有兩個文件,我想逐行閱讀(第一個包含每行一個單詞,第二個每行一個句子)。逐行讀取文件
目標是計算句子的數量從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";
}
,但只對第一個文件的第一個字的處理!
如果您要求大量的人閱讀並理解您的代碼,那麼儘可能讓它閱讀起來很簡單。我已經做了一些輕量級的重新格式化,以添加一些縮進,並使您對空白的使用更加均勻。請在將來自己做。 –