in reply to Re^2: grep unique values , remove the blank spaces and store it in a variable
in thread grep unique values , remove the blank spaces and store it in a variable

add another condition to the if/unless

#!/usr/bin/perl use strict; use XML::Simple; my $xml = do {local $/='';<DATA>} ; my $servers = XMLin($xml); my %seen=(); my @array=(); foreach my $server (@{$servers->{server}}) { my $lanip = $server->{LanIP}; $lanip =~s/\d+$/0/; push(@array, $lanip) unless ( $seen{$lanip}++ || $lanip eq ''); } print join "\n",@array; __DATA__ <xml> <server LanIP="192.169.30.123"/> <server LanIP=""/> <server LanIP="192.169.30.24"/> <server LanIP=""/> <server LanIP="192.169.31.126"/> <server LanIP="192.169.32.127"/> </xml>
poj
  • Comment on Re^3: grep unique values , remove the blank spaces and store it in a variable
  • Download Code

Replies are listed 'Best First'.
Re^4: grep unique values , remove the blank spaces and store it in a variable
by brother_m (Initiate) on Sep 15, 2015 at 09:40 UTC

    The substitution of 0 is indeed mysterious, and indeed substr would not help for 192.169.30.24.

    Probably the programmer would like to split network number from host part for IPv4 addresses. Dotted quad notation is for the readers convenience, manipulation is better done in binary. Replacing the last quad only works for a mask of 255.255.255.0 and 192.169.30.214/28 would not be split properly.
    The following strips off the host part, but would not work for IP v6
    use Socket; ... my $addr = $server->{LanIP}; my $mask = my $mask = $server->{Netmask}; my $netaddr = inet_ntoa(inet_aton($mask) & inet_aton($addr)); push(@array, $netaddr) unless $seen{$netaddr}++;