Hello Perl guru's
Please help me out to fix the below error
Can't use an undefined value as an ARRAY reference at Test_Blank.pl line 180
i am comparing the two files (old & New) and traking the number of change records in both files
here is the code
#!/usr/local/bin/perl
use strict;
use warnings;
use Config;
use Getopt::Std;
#use 5.010;
use Data::Dumper;
use diagnostics;
my $filename_old = '/opt/bin/test_old';
my $filename_new = '/opt/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; print "Old: @{$moved{'from'}->{$v}->{'group'}}, $g \n";
#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; print "New: @{$moved{'to'}->{$v}->{'group'}}, $g \n";
# 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;
my $group = "";
my $value=0;
my $i;
my $array_size;
open my $fh, '<', $filename or die "Failed to open $filename: $!";
while (<$fh>){
my $str_Line = $_;
chomp;
next if m/^##/; #Skip commented-out data lines
next unless m/\d{3}/;
#print "Line $str_Line \n";
if($str_Line =~ /Component*/)
{
next;
}
if($str_Line =~ /NetVault 8.22 Server "nvmaster"*/)
{
next;
}
# if($str_Line =~ s/Unknown/Blank/)
# {
# next;
# }
my @array_new = split(" ",$str_Line);
$array_size = @array_new;
$array_size = scalar(@array_new);
$array_size = $#array_new+1;
#print "size $array_size \n";
if($array_size > 2)
{
for($i = 0;$i<$array_size-1;$i++)
{
if ($i == 0)
{
$group = $array_new[$i];
}
else
{
$group = $group." ".$array_new[$i];
}
}
$value = $array_new[$array_size-1];
$data{$filename}->{$value}->{$group}->{'count'}++;
}
# print "\n";
#print $group."1";
# $data{$filename}->{$value}->{$group}->{'count'}++;
if($array_size <= 2)
{
# my ($group, $value) = split;
$group = $array_new[0];
# print "\n";
$value = $array_new[1];
$data{$filename}->{$value}->{$group}->{'count'}++;
}
#print "group $group MediaId $value \n";
#print $group;
}
}
=head
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'}++;
}
}
=cut
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'}; print "MeidaID : $v";
my @gf = @{$moved{'from'}->{$k}->{'group'}}; print "loop from : @{$moved{'from'}->{$k}->{'group'}} \n";
my @gt = @{$moved{'to'}->{$k}->{'group'}}; print "loop New : @{$moved{'to'}->{$k}->{'group'}} \n";
# say "Media(Tape) $v from group @gf has been moved to @gt group";
format LOGON_new_TOP =
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 =
Analysis
-----------------------------------------------------
.
format LOGON_Data =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
"No one used ",
.
write(LOGON_Data);
close LOGON_Data;
}
close LOGON_new;
Thanks in advance