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;
}

Edited 5 Years Ago by realoneomer: n/a

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.

Here are sample test_old.txt and test_new.txt

test_old.txt

ABC_DEF 123
ABC_DEF 432
ABC_DEF 023
GTY_ERT 890
GTY_ERT 980
GTY_ERT 431
TYU_POI 086
TYU_POI 542
TYU_POI 079
TYU_POI 689
RED_FRW 341
RED_FRW 435
RED_FRW 980
RED_FRW 879
WQA_UIY 125
WQA_UIY 088
WQA_UIY 012
YUI RET 657
YUI RET 456
YUI RET 107
YUI RET 450


test_new.txt

ABC_DEF 123
ABC_DEF 432
ABC_DEF 023
GTY_ERT 890
GTY_ERT 980
GTY_ERT 431
TYU_POI 086
TYU_POI 450
TYU_POI 107
TYU_POI 542
TYU_POI 079
TYU_POI 689
RED_FRW 341
RED_FRW 435
RED_FRW 980
RED_FRW 879
WQA_UIY 125
WQA_UIY 088
WQA_UIY 012
YUI RET 657
YUI RET 456

Now you can see two values from YUI RET has been moved under TYU_POI group i want to track these medias

I think the problem is with the split operator when i am spliting a files in two columns like group and values it is creating an error on YUI RET because split operator considers this value as two values and when i am comparing the values it is unable to find such kind of group in a file please help me that how i can fix this issue


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;
}
This article has been dead for over six months. Start a new discussion instead.