
To compare two files and search for a word in both files
Quote:
> I am working on one perl script (on windows).
> I have two files- the first one with hunded lines and
> the second one with 5 lines (5 words (one per line),
> actaully). I have to compare the contents of the
> second file with those of first file.
> If any of the 5 words in the second file is not found
> in the first file of 100 lines, I should print that
> that specific word is not found in the first file. Say, if 3 out of 5
> words (from the second file) are not found in the first file, it
> should print all of them as the output.
[snip]
> I tried to code, but it was not giving me the desired
> result - some thing wrong in the foreach and while
> loops.
Try showing us the code.
Quote:
> Would one of you kindly come up with the code for this
> scenario?
Fine:
# This code is Copyright (c) 2003 Bryan Donlan
# This code is for demonstrative purposes only
# You may not use this in your homework, or for
# any commercial uses.
open WORDS,"<$fileone" or die "Opening $fileone: $!\n";
open SEARCH,"<$filetwo" or die "Opening $filetwo: $!\n";
my %words;
chomp $word;
$words{$word} = 1;
Quote:
}
while(my $line = <SEARCH>){
if(index($line,$word) != -1){
delete $words{$word};
}
}
Quote:
}
foreach $word (keys %words){
print "$word not found\n";
Quote:
}
close WORDS;
close SEARCH;
--
Replace spamtrap with bd to reply.
A shapely CATHOLIC SCHOOLGIRL is FIDGETING inside my costume..