Reputation: 891
I want to compare two text files that i have generated from one of the perl script that i wrote. I want to print out the matched results from those two text files. I tried looking at couple of answers and questions that people have asked on stackoverflow but it does not work for me. Here is what i have tried.
my $file1 = "Scan1.txt";
my $file2 = "Scan2.txt";
my $OUTPUT = "final_result.txt";
my %results = ();
open FILE1, "$file1" or die "Could not open $file1 \n";
while(my $matchLine = <FILE1>)
{
$results{$matchLine} = 1;
}
close(FILE1);
open FILE2, "$file2" or die "Could not open $file2 \n";
while(my $matchLine =<FILE2>)
{
$results{$matchLine}++;
}
close(FILE2);
open (OUTPUT, ">$OUTPUT") or die "Cannot open $OUTPUT \n";
foreach my $matchLine (keys %results) {
print OUTPUT $matchLine if $results{$matchLine} ne 1;
}
close OUTPUT;
EXAPLE OF OUTPUT THAT I WANT
FILE1.TXT data 1 data 2 data 3
FILE2.TXT data2 data1
OUTPUT data 1 data 2
Upvotes: 2
Views: 12863
Reputation: 39763
Your problem is that your hash now has following states:
This ambiguity will make your check (hash ne 1) fail.
The minimal required change to your algorithm would be:
my $file1 = "Scan1.txt";
my $file2 = "Scan2.txt";
my $OUTPUT = "final_result.txt";
my %results = ();
open FILE1, "$file1" or die "Could not open $file1 \n";
while(my $matchLine = <FILE1>)
{
$results{$matchLine} = 1;
}
close(FILE1);
open FILE2, "$file2" or die "Could not open $file2 \n";
while(my $matchLine =<FILE2>)
{
$results{$matchLine} = 2 if $results{$matchLine}; #Only when already found in file1
}
close(FILE2);
open (OUTPUT, ">$OUTPUT") or die "Cannot open $OUTPUT \n";
foreach my $matchLine (keys %results) {
print OUTPUT $matchLine if $results{$matchLine} ne 1;
}
close OUTPUT;
Upvotes: 1