deep27ak has asked for the wisdom of the Perl Monks concerning the following question:

Hello All, I have quite a confusing and complicated situation and let me tell you I am not so expert on scripting. I have an xml file with below contents
<server NodeName__1="blr92-nds-rds" CoreLanNum="8" CoreLanIP_1__1="192.169.72.3" CoreLanNetmask_1="255.255.255.0" CoreLanIP_2__1="192.169.72.3" CoreLanIP_3__1="192.169.75.1" CoreLanNetmask_3="255.255.255.0" CoreLanIP_4__1="192.169.75.1" CoreLanNetmask_4="255.255.255.0" CoreLanIP_5__1="192.169.74.1" CoreLanNetmask_5="255.255.255.0" CoreLanIP_6__1="192.169.74.1" CoreLanNetmask_6="255.255.255.0" BR_IP="192.169.73.3" BRLAN="y" CoreLanIP_7__1="192.169.73.3" CoreLanNetmask_7="255.255.255.0" CoreLanIP_8__1="192.169.73.3" CoreLanNetmask_8="255.255.255.0" RouteIP_1__1="192.169.75.0" RouteNetmask_1__1="255.255.255.0 </server>
I have below script
#!/usr/bin/perl -w use strict; use XML::Simple; my $servers = XMLin('network.xml'); my %seen; foreach my $server (@{$servers->{server}}) { my @node = $server->{NodeName__1} . "\n"; my $lanip = $server->{CoreLanIP_1__1} ; my @mask = $server->{CoreLanNetmask_1} . "\n"; my @ip = split('\.', $lanip); my @newcoreip = join (".", $ip[0], $ip[1], $ip[2], 0); my $network = join("/", @newcoreip, @mask); push(my @networklist, $network) if ! $seen{$network}++; my $brlan = $server->{BRLAN} ; if ( $brlan eq "y" ) { my $brip = $server->{BR_IP}. "\n" ; my $corelanip = $server->{CoreLanIP_2__1} . "\n"; my @corelannum = $server->{CoreLanNum} ."\n" ; foreach $a (@corelannum) { for i in $a --> (stucked here) } } }
Here basically I have to first lookout for 'CoreLanNum' and suppose if its value is 8 then the loop should run for 8 times where $i will be 1...8 in 'CoreLanIP_$i__1' and CoreLanNetmask_$i . Next each value of CoreLanIP_$i__1 should be checked with $brip and the one which matches like for above BR_IP="192.169.73.3" matches with CoreLanIP_7__1="192.169.73.3" then store CoreLanNetmask_7 into a variable

My question here how can I select 'CoreLanIP_$i__1' from the xml since $i is a changing variable and the same goes for CoreLanNetmask_$i

and how should I use a loop in a way for what I want to achieve.

I am sorry its pretty confusing but I hope someone can help...I don't expect someone to do my homework but if atleast you can help me how to start and what to look for here.

Some help would be very much appreciated.

Replies are listed 'Best First'.
Re: need help with for loop within an xml
by poj (Abbot) on Sep 14, 2015 at 18:56 UTC
    #!/usr/bin/perl use strict; use XML::Simple; use Net::Netmask; my @block; my %seen; my $xml = do {local $/='';<DATA>} ; my $servers = XMLin($xml, ForceArray => 1); foreach my $server (@{$servers->{server}}) { my $br_ip = $server->{BR_IP}; my $num = $server->{CoreLanNum}; #print "Searching for $br_ip\n";; for my $n (1..$num){ my $ip = $server->{'CoreLanIP_'.$n.'__1'}; if ($ip eq $br_ip){ my $mask = $server->{'CoreLanNetmask_'.$n}; # do what you want here with $n $ip or $mask my $block = new Net::Netmask($ip,$mask); push @block,$block unless $seen{$block}++; } } } print $_."\n" for @block; __DATA__ <xml> <server NodeName__1="blr92-nds-rds" CoreLanNum="8" CoreLanIP_1__1="192.169.72.3" CoreLanNetmask_1="255.255.255.0" CoreLanIP_2__1="192.169.72.3" CoreLanIP_3__1="192.169.75.1" CoreLanNetmask_3="255.255.255.0" CoreLanIP_4__1="192.169.75.1" CoreLanNetmask_4="255.255.255.0" CoreLanIP_5__1="192.169.74.1" CoreLanNetmask_5="255.255.255.0" CoreLanIP_6__1="192.169.74.1" CoreLanNetmask_6="255.255.255.0" BR_IP="192.169.73.3" BRLAN="y" CoreLanIP_7__1="192.169.73.3" CoreLanNetmask_7="255.255.255.0" CoreLanIP_8__1="192.169.73.3" CoreLanNetmask_8="255.255.255.0" RouteIP_1__1="192.169.75.0" RouteNetmask_1__1="255.255.255.0"> </server> </xml>
    poj
      Thanks very much for your reply. I understood this loop
      for my $n (1..$num){ my $ip = $server->{'CoreLanIP_'.$n.'__1'}; if ($ip eq $br_ip){ my $mask = $server->{'CoreLanNetmask_'.$n};

      which did solve one of my problem of searching all the values as per 'corelannum'

      As of now
      foreach my $n (1..$num){ my $corebrip = $server->{'CoreLanIP_'.$n.'__1'} ; my $brmask = $server->{'CoreLanNetmask_'.$n} ."\n"; if ($corebrip eq $br_ip){ print join ("/", $br_ip, $brmask);
      I get the below output
      192.169.73.1/255.255.255.0 192.169.73.1/255.255.255.0 192.169.73.2/255.255.255.0 192.169.73.2/255.255.255.0 192.169.73.3/255.255.255.0 192.169.73.3/255.255.255.0 192.169.73.4/255.255.255.0 192.169.73.4/255.255.255.0 192.169.73.5/255.255.255.0 192.169.73.5/255.255.255.0 192.169.73.6/255.255.255.0 192.169.73.6/255.255.255.0
      so it is searching the complete xml at once as the data I have posted is only one block...so How can I make the script to search block by block?

      For each block it should search for all these variables and perform as I say then move to next block as I guess that would simplify things?

      like for each block I will have different br_ip, CoreLanIP_'.$n.'__1' and  CoreLanNetmask_'.$n

      So for each block it should search corelanip and if it matches br_ip then use the equivalent corelannetmask

        for each block I will have different br_ip

        I don't understand what you mean, your output has different br_ips. Un-comment this to see each block being searched

         #print "Searching for $br_ip\n";
        poj