new_perl_user
asked on
Logic not working in the perl script
Hi, Below is the perl script which compares data between two files. If data in file 1 exsists in file2 fine, else if not exsists it should print the id's that are missing from file 2.
Issue: It is comparing file1 and file2 and also printing data not found, if they don't exist in file1 but it has to always print not found from file2.
Can anyone pls help me where I am doing the mistake.
Issue: It is comparing file1 and file2 and also printing data not found, if they don't exist in file1 but it has to always print not found from file2.
Can anyone pls help me where I am doing the mistake.
#!/usr/local/bin/perl
use strict;
use warnings;
my $date = localtime(time()-48*60*60);
substr($date, 11, 9) = "";
print"$date\n";
my($sec, $min, $hour, $mday, $mon, $year) = localtime;
my $logFilename = sprintf("sync_DB_%d%02d%02d.txt", 1900+$year, $mon+1, $mday, $hour, $min, $sec);
open(LOG, ">/usr/hlm/qa/Scripts/nhl/STATS/LOGS/$logFilename");
open IN, '</usr/hlm/qa/Scripts/nhl/STATS/LOGS/DB/Scanner_Stats.txt' or die "could not open out.txt: $!";
my %hash1 = map { chomp; $_ => 1 } <IN>;
close IN;
open IN, '</usr/hlm/qa/Scripts/nhl/STATS/LOGS/sync_Stats.txt' or die "could not open sample.txt: $!";
my %hash2;
@hash2{ map{/(\S+)/} <IN>}=();
close IN;
if($hash1{$key1} eq $hash2{$key2})
{
print LOG 'FOLDER Total = '.keys %hash1,"\n\n", 'Linux FOLDER2 Total = '.keys %hash2,"\n\n";
print LOG "List of not found IDS :
",(join"\n ",@id),"\n\n" if @id=grep!(exists $hash2{$_}&&exists $hash1{$_}),keys %hash1,
keys %hash2;
}
If data in file 1 exsists in file2 fine, else if not exsists it should print the id's that are missing from file 2.I'm confused.
Issue: It is comparing file1 and file2 and also printing data not found, if they don't exist in file1 but it has to always print not found from file2.
What do you want to do if data exists in file1 but not in file2?
What do you want to do if data exists in file2 but not in file1?
ASKER
Hi,
I want,
if data exists in file1 but not in file2? : print out the data that is not in file2 under: List of not found IDS :
if data exists in file 2 but not in file1 : print out the data that is not in file1 under: List of not found IDS : plus add a statement as missing from scanner.
For ex: 32714373248 - Missing from scanner.
Thanks,
I want,
if data exists in file1 but not in file2? : print out the data that is not in file2 under: List of not found IDS :
if data exists in file 2 but not in file1 : print out the data that is not in file1 under: List of not found IDS : plus add a statement as missing from scanner.
For ex: 32714373248 - Missing from scanner.
Thanks,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
Thank you so much it is working. One small help, is it possible to do sorting for the IDS when printing (ascending order).
Thank you so much it is working. One small help, is it possible to do sorting for the IDS when printing (ascending order).
print LOG "List of not found IDS :
",(join"\n",@id),"\n\n" if @id=grep!exists $hash2{$_},sort keys %hash1;
",(join"\n",@id),"\n\n" if @id=grep!exists $hash2{$_},sort keys %hash1;
Why not either
%hash1 = map { chomp; $_ => 1 } <IN>;
...
%hash2 = map { chomp; $_ => 1 } <IN>;
or
@hash1{ map{/(\S+)/} <IN>}=();
...
@hash2{ map{/(\S+)/} <IN>}=();
if($hash1{$key1} eq $hash2{$key2}) #where did $key1 and $key2 come from? What is this meant to check for?