in reply to Summarizing an array of IP's
use strict; use warnings; my %ips; my @array = qw ( 10.0.0.1 10.0.0.2 10.0.0.3 192.168.1.5 192.168.1.6 192.168.10.3 192.168.10.4 192.168.10.5 10.0.0.1); foreach (@array) { $ips{$1}{$2} = undef if /([\d.]+)\.(\d+)$/; } foreach ( keys %ips ) { my $keys = keys %{$ips{$_}}; print "$_: $keys nodes\n" }
If by nodes you meant values appearing regardless of possible duplication:
use strict; use warnings; my %ips; my @array = qw ( 10.0.0.1 10.0.0.2 10.0.0.3 192.168.1.5 192.168.1.6 192.168.10.3 192.168.10.4 192.168.10.5 10.0.0.1); foreach (@array) { $ips{$1}++ if /([\d.]+)\.(\d+)$/; } foreach ( keys %ips ) { print "$_: $ips{$_} nodes\n" }
(Note the different value of 10.0.0 caused by the extra 10.0.0.1 in the array)
-enlil
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Summarizing an array of IP's
by carric (Beadle) on Jan 06, 2004 at 22:20 UTC |