hi Perl Gurus,
I have two text files namely test_old and test_new and i am using below perl script which compares both files (old & new) and display only those records which only have changed values but i am getting an error like
Can't use an undefined value as an ARRAY reference at test_Blank.pl line 89.
Value of old file a1
Value of new file b0
Value of old file a1
Value of new file b0
Value of old file a1
Value of new file b0
Value of old file a1
Value of new file b0
Value of old file a1
Value of new file b0
Value of old file a1
Value of new file b0
Value of old file a1
Value of new file b0
Value of old file a1
Value of new file b0
Value of old file a1
Value of new file b0
Value of old file a1
Value of new file b0
Value of old file a1
Value of new file b0
Value of old second file c0
Value of new second file d11
Can't use an undefined value as an ARRAY reference at test_Blank.pl line 89.
Could you please help me out to fix this issue..?
here is a perl code.
#!/usr/local/bin/perl
use strict;
use warnings;
use Config;
use Getopt::Std;
#use 5.010;
use Data::Dumper;
my $filename_old = '/opt/backup/bin/test_old';
my $filename_new = '/opt/backup/bin/test_new';
my %data;
my %moved;
my $a=0; my $b = 0 ; my $c=0; my $d=0;
read_file($filename_old);
read_file($filename_new);
#Find lines moved from old
foreach my $v(keys %{$data{$filename_old}}){
foreach my $g(keys %{$data{$filename_old}->{$v}}){
$data{$filename_new}->{$v}->{$g}->{count} = 0
unless defined $data{$filename_new}->{$v}->{$g}->{count};
if ($data{$filename_old}->{$v}->{$g}->{count}
> $data{$filename_new}->{$v}->{$g}->{count}) {
$moved{'from'}->{$v}->{'value'} = $v;
$moved{'from'}->{$v}->{'group'} = []
unless defined $moved{'from'}->{$v}->{'group'};
push @{$moved{'from'}->{$v}->{'group'}}, $g;
#say "$v $g count is $data{$filename_old}->{$v}->{$g}->{count}";
$a = $data{$filename_old}->{$v}->{$g}->{count};
print "Value of old file a".$a; print "\n";
#
# say "$filename_new $v $g count is $data{$filename_new}->{$v}->{$g}->{count}";
$b = $data{$filename_new}->{$v}->{$g}->{count};
print "Value of new file b".$b;print "\n";
}
}
}
#Find lines moved to new
foreach my $v(keys %{$data{$filename_new}}){
foreach my $g(keys %{$data{$filename_new}->{$v}}){
$data{$filename_old}->{$v}->{$g}->{count} = 0
unless defined $data{$filename_old}->{$v}->{$g}->{count};
if ($data{$filename_new}->{$v}->{$g}->{count}
> $data{$filename_old}->{$v}->{$g}->{count}) {
$moved{'to'}->{$v}->{'value'} = $v;
$moved{'to'}->{$v}->{'group'} = []
unless defined $moved{'to'}->{$v}->{'group'};
push @{$moved{'to'}->{$v}->{'group'}}, $g;
# say "$v $g count is $data{$filename_old}->{$v}->{$g}->{count}";
$c = $data{$filename_old}->{$v}->{$g}->{count};
print "Value of old second file c".$c;print "\n";
# say "$filename_new $v $g count is $data{$filename_new}->{$v}->{$g}->{count}";
$d = $data{$filename_new}->{$v}->{$g}->{count};
print "Value of new second file d".$d;print "\n";
}
}
}
sub read_file {
my $filename = shift;
open my $fh, '<', $filename or die "Failed to open $filename: $!";
while (<$fh>){
chomp;
next if m/^##/; #Skip commented-out data lines
next unless m/\d{3}/;
my ($group, $value) = split;
$data{$filename}->{$value}->{$group}->{'count'}++;
}
}
open (LOGON_new,">>",'Data_File.txt') or die "Could not open file Data_File.txt:$!";
#while ( ($k,$v) = each %hashGroups )
#{
# print "Groups: $k => count: $v\n";
#}
#if ($a ne $d || $d ne $a && $c eq $b || $b eq $c)
#{
foreach my $k(sort keys %{$moved{'from'}}){
my $v = $moved{'from'}->{$k}->{'value'};
my @gf = @{$moved{'from'}->{$k}->{'group'}};
my @gt = @{$moved{'to'}->{$k}->{'group'}};
# say "Media(Tape) $v from group @gf has been moved to @gt group";
format LOGON_new_TOP =
Netvault Blank Media Analysis
Media Id Moved from Moved to
-----------------------------------------------------
.
format LOGON_new =
@<<<<<<<<<<<<<< @<<<<<<<<<<<<<< @<<<<<<<<<<<<<<
$v, @gf, @gt
.
write(LOGON_new);
}
#}
if ($a eq $c && $b eq $d)
{
open (LOGON_Data,">>",'Data_File.txt') or die "Could not open file Data_File.txt:$!";
format LOGON_Data_TOP =
Netvault Blank Media Analysis
-----------------------------------------------------
.
format LOGON_Data =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
"No Blank Media/ Tape was used ",
.
write(LOGON_Data);
close LOGON_Data;
}