#!/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

In reply to Re: need help with for loop within an xml by poj
in thread need help with for loop within an xml by deep27ak

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.