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

Welcome to the Monastery, deep27ak!

At a glance, it appears as though you've got numerous issues with your code, including undefined variables, an erroneous }, repeatedly reassigning over a variable without using it in loops etc.

Please post enough of a sample of the XML file you're using as input that will satisfy a single pass of the script. Without that, it's too time consuming to reconstruct the code properly.

Also, get in the habit of using proper indenting. It'll help you greatly, and it'll help us work with what you've got.

Here's an example:

use strict; use warnings; use XML::Simple; my $xml = do {local $/='';'network.xml'} ; my $servers = XMLin($xml, ForceArray => 1); my %seen; foreach my $server (@{$servers->{server}}) { my $brlist; my $corebrip; my $brmask; my $brip; my $num; my $brlan; my @node = $server->{NodeName__1} . "\n"; my $lanip = $server->{CoreLanIP_1__1} ; my @mask = $server->{CoreLanNetmask_1} . "\n"; my $network = join("/", $lanip, @mask); $network =~ s/.[0-9]*\//.0\//g; push(my @networklist, $network) if ! $seen{$network}++; $brlan = $server->{BRLAN} ; if ( $brlan eq "y" ) { $brip = $server->{BR_IP}; $num = $server->{CoreLanNum} ."\n" ; } my @brnwlist; for my $n (1..$num){ $corebrip = $server->{'CoreLanIP_'.$n.'__1'} ; $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"; } } for $a (@brnwlist) { if ( $a ne "") { print "empty array"; my $vnum = $server->{CoreVlanNum} ."\n" ; for my $i (1..$vnum){ my $vlanip = $server->{'CoreVlan_IP_'.$i} ; my $vmask = $server->{'CoreVlan_Netmask_'.$i} ."\n"; if ($vlanip eq $brip){ my $vlist = join ("/", $brip, $vmask) ; $vlist =~ s/.[0-9]*\//.0\//g; push(my @vnwlist, $vlist) if ! $seen{$vlist}++; print @vnwlist; } } } }

-stevieb

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 15, 2015 at 17:25 UTC

    Thanks very much for looking into this. Yes I understand it has multiple issues and I am still trying to evolve on scripting so I sure WILL take your advice in upcoming posts

    I have updated my question a trimmed version of xml although it contains all the necessary input required for the script.

    if BRLAN = y then first the script should search for CoreLanNum $n. and the loop should run as many times as $n and should match BR_IP with CorLanNum_IP and store the value in a array.

    If this array is empty run next for loop and search of CoreVlanNum $n and the script should again search for CoreVlanIP with BR_IP and map it with equivalent netmask.

      For the first server mch30-nds-ins there in no BR_IP attribute to match against.

      poj
        Thanks for looking into this, YES since that is the primary condition is br_ip is YES then move ahead or else use @networklist which I have created at the start of my foreach loop