Avatar of new_perl_user
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.

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

Perl

Avatar of undefined
Last Comment
ozo

8/22/2022 - Mon
ozo

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

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?
new_perl_user

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,
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
ozo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
new_perl_user

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).
ozo

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