Logic not working in the perl script

new_perl_user
new_perl_user used Ask the Experts™
on
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.

#!/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;
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
why the inconsistency defining the hashes?
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?
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
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.
I'm confused.
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?

Author

Commented:
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,
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Most Valuable Expert 2014
Top Expert 2015
Commented:
print LOG "List of not found IDS :
",(join"\n",@id),"\n\n" if @id=grep!exists $hash2{$_},keys %hash1;
print LOG "List of not found IDS :
",(join"- Missing from scanner.\n",@id,"\n") if @id=grep!exists $hash1{$_},keys %hash2;

Author

Commented:
Hi,

Thank you so much it is working. One small help, is it possible to do sorting for the IDS when printing (ascending order).
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
print LOG "List of not found IDS :
",(join"\n",@id),"\n\n" if @id=grep!exists $hash2{$_},sort keys %hash1;

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial