in reply to Use of uninitialized value $brip in join or string at script.pl line 50

stevieb's approach is probably the way to go, however it assumes that if BR_IP = "n" then you go and search CoreVlanIP for matches. In the code below I assume that if BR_IP = "n" then do nothing, but if the @brnwlist is empty then search CoreVlanIP for matches. I have added some print statements and use Data::Dumper to show what is going on (a habit worth acquiring):
use Data::Dumper; my $count = 1; foreach my $server (@{$servers->{server}}) { print "\nCount: ", $count++, "\n"; my $brlan = $server->{BRLAN} ; print "brlan = $brlan\n"; next unless $brlan eq "y"; my $brip = $server->{BR_IP}; my $num = $server->{CoreLanNum}; my $corebrip = $server->{'CoreLanIP_'.$num.'__1'} ; my $brmask = $server->{'CoreLanNetmask_'.$num}; my @brnwlist; for my $n (1..$num){ $corebrip = $server->{'CoreLanIP_'.$n.'__1'} ; if (not $corebrip) { print "CoreLanNum $n - No value for CoreLanIP_".$n."__1\n" +; next; } print "CoreLanNum $n - brip: $brip, corebrip: $corebrip\n"; $brmask = $server->{'CoreLanNetmask_'.$n}; if ( $corebrip eq $brip ){ my $brlist = join ("/", $brip, $brmask) ; $brlist =~ s/.[0-9]*\//.0\//g; push( @brnwlist, $brlist) unless $seen{$brlist}++; } else { print "coreip does not match brip\n"; } } print "brnwlist: ", Dumper(\@brnwlist); next if @brnwlist; my @vnwlist; my $vnum = $server->{CoreVlanNum}; for my $i (1..$vnum){ my $vlanip = $server->{'CoreVlan_IP_'.$i}; if (not $vlanip) { print "CoreVlanNum $i - No value for CoreVlan_IP_$i\n"; next; } print "CoreVlanNum $i - brip: $brip, vlanip: $vlanip\n"; my $vmask = $server->{'CoreVlan_Netmask_'.$i}; if ($vlanip eq $brip){ my $vlist = join ("/", $brip, $vmask) ; $vlist =~ s/.[0-9]*\//.0\//g; push(my @vnwlist, $vlist) if ! $seen{$vlist}++; } else { print "vlanip does not match brip\n"; } } print "vnwlist: ", Dumper(\@vnwlist); }
Output:
Count: 1 brlan = n Count: 2 brlan = y CoreLanNum 1 - brip: 192.168.165.16, corebrip: 192.168.156.16 coreip does not match brip CoreLanNum 2 - brip: 192.168.165.16, corebrip: 192.168.156.16 coreip does not match brip brnwlist: $VAR1 = []; CoreVlanNum 1 - brip: 192.168.165.16, vlanip: 192.168.156.16 vlanip does not match brip CoreVlanNum 2 - No value for CoreVlan_IP_2 vnwlist: $VAR1 = []; Count: 3 brlan = y CoreLanNum 1 - brip: 192.169.73.1, corebrip: 192.169.32.1 coreip does not match brip CoreLanNum 2 - brip: 192.169.73.1, corebrip: 192.169.32.1 coreip does not match brip CoreLanNum 3 - brip: 192.169.73.1, corebrip: 192.169.73.1 CoreLanNum 4 - brip: 192.169.73.1, corebrip: 192.169.73.1 brnwlist: $VAR1 = [ '192.169.73.0/255.255.255.240' ];

Replies are listed 'Best First'.
Re^2: Use of uninitialized value $brip in join or string at script.pl line 50
by deep27ak (Novice) on Sep 16, 2015 at 06:38 UTC

    Thanks very much stevieb and tangent.

    You guys made my day. I now understand the usage of variables and the place it should be defined. I did tried using subroutines but I believe my script requirement would be fulfilled more by the second method.

    Till now the script does works as expected with one loop hole as I see.

    After picking up unique values the output contains empty lines as below

    If vlannum exists
    vnwlist: $VAR1 = [ '192.168.165.0/255.255.255.0 ' ]; vnwlist: $VAR1 = []; vnwlist: $VAR1 = []; vnwlist: $VAR1 = []; vnwlist: $VAR1 = []; vnwlist: $VAR1 = []; vnwlist: $VAR1 = []; vnwlist: $VAR1 = [];
    if corelannum exists
    brnwlist: $VAR1 = [ '192.169.73.0/255.255.255.0 ' ]; brnwlist: $VAR1 = []; brnwlist: $VAR1 = []; brnwlist: $VAR1 = []; brnwlist: $VAR1 = []; brnwlist: $VAR1 = [];
    I tried various provided here http://www.perlmonks.org/?node_id=124970

    but nothing seems to work in my case. At one point I have to dump these values in a file and with these empty lines it will not be proper.

    Also can someone please help me understand why do we use Data:Dumper ?

    my script
    for my $n (1..$num){ $corebrip = $server->{'CoreLanIP_'.$n.'__1'} ; if (not $corebrip) { print "CoreLanNum $n - No value for CoreLanIP_".$n."__1\n" +; next; } # print "CoreLanNum $n - brip: $brip, corebrip: $corebrip\n"; $brmask = $server->{'CoreLanNetmask_'.$n} . "\n" ; if ( $corebrip eq $brip ){ $brlist = join ("/", $brip, $brmask) ; $brlist =~ s/.[0-9]*\//.0\//g; push( @brnwlist, $brlist) unless $seen{$brlist}++; } else { # print "coreip doesnot matches brip\n"; } } print "brnwlist: ", Dumper(\@brnwlist); next if @brnwlist; my @vnwlist; my $vnum = $server->{CoreVlanNum}; for my $i (1..$vnum){ my $vlanip = $server->{'CoreVlan_IP_'.$i} ; if (not $vlanip) { # print "CoreVlanNum $i - No value for CoreVlan_IP_$i\n"; next; } # print "CoreVlanNum $i - brip: $brip, vlanip: $vlanip\n"; my $vmask = $server->{'CoreVlan_Netmask_'.$i} . "\n"; if ($vlanip eq $brip){ my $vlist = join ("/", $brip, $vmask) ; $vlist =~ s/.[0-9]*\//.0\//g; push @vnwlist, $vlist unless $seen{$vlist}++; } } @vnwlist = grep (/\S/, @vnwlist); print "vnwlist: ", Dumper(\@vnwlist); } }