Hey all.
Okay I am kind of new to PERL.
I have 2 files A.txt and B.txt now I compare A.txt to B.txt and if file B.txt doesn’t have the word in it that file A.txt has, file B.txt gets updated with the new word from file A.txt
This is the problem that I have, file A.txt has the word AAAS duplicated a number of times and file B.txt does not have the word in it.
The first time around B.txt gets updated with the new word AAAS and then when A.txt compares AAAS in B.txt, B.txt gets updated again with the word AAAS.
Duplicating the word AAAS in file B.txt as well.
How do I stop this from happening??
Following is the code that i am using. . .
my($comepare_word);
my($compare_dic_word);
my($flag) = "false";
sub trim($)
{
my $string = $_;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
open compareWord, "<A.txt" or die "Main file cannot be opend $!";
while(<compareWord>) {
chomp;
$comepare_word = trim($_);
$first_char = substr($comepare_word, 0, 1);
if ($first_char eq "a" || $first_char eq "A" ) {
open compareDic, "<B.txt" or die "B.txt cannot be opend $!";
while(<compareDic>) {
chomp;
$compare_dic_word = trim($_);
if($comepare_word eq $compare_dic_word) {
$flag = "true";
last;
} else {
$flag = "false";
}
}
if($flag eq "false"){
open outFile, ">>B.txt" or die "OUT cannot be opend $!";
print outFile "$comepare_word\n";
$flag = "false";
}
}
}