Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: grep unique values , remove the blank spaces and store it in a variable

by Anonymous Monk
on Sep 14, 2015 at 07:22 UTC ( [id://1141885]=note: print w/replies, xml ) Need Help??


in reply to grep unique values , remove the blank spaces and store it in a variable

Also, this is interesting :)  [perldoc://unique] unique -> perlfaq4#How can I remove duplicate elements from a list or array?

Use a hash. When you think the words "unique" or "duplicated", think "hash keys".
  • Comment on Re: grep unique values , remove the blank spaces and store it in a variable
  • Download Code

Replies are listed 'Best First'.
Re^2: grep unique values , remove the blank spaces and store it in a variable
by deep27ak (Novice) on Sep 14, 2015 at 07:30 UTC
    Thanks for the reply, I tried that method and it works the same way as it is now. The unique values are getting captured but there are a bunch of empty lines
    push(my @array, $lanip) if ! $seen{$lanip}++; #print "@array\n"; my %hash = map { $_, 1 } @array; my @unique = keys %hash; print "@unique\n";
    output
    192.169.30.0 192.169.31.0 192.169.32.0 192.169.72.0
    How can I remove them?

      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

        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}++;
      Hello if you want to filter out, let's say, keys that does not starts whit number, you can change
      my %hash = map { $_, 1 } @array;
      with
      my %hash = map { $_, 1 } grep {$_=~/^\d/} @array;

      Anyway if i understand it must be only one key like this. maybe better you use Data::Dump's dd to inspect the array.

      Also some garbage can come from (deprecated) XML::Simple. Try to switch to something usable like XML::Twig.

      UPDATE: obviously the sage Athanasius is right here below, code does not make much sense: but i cant find where the foreach loop ends, too. Also the substr($lanip, 11, 3) = "0"; part seems very erro prone, maybe something like  s/\d{1,3}$/0/ is better (if i understand the will of OP)
      HtH
      L*
      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re^2: grep unique values , remove the blank spaces and store it in a variable
by brother_m (Initiate) on Sep 15, 2015 at 09:04 UTC
    Actually the seeker's code preserves the input order, but hash would not.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1141885]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-23 21:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found