use Socket; use strict; use warnings; my %stat; my @addr = 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); $stat{inet_ntoa(substr(inet_aton($_), 0, 3) . "\0")} ++ foreach (@addr); print "$_ has $stat{$_} nodes\n" foreach (keys %stat);