$ perl -le' my %hashIPs = qw( 192.168.1.0 1 192.168.1.1 1 192.168.1.2 1 192.168.1.4 1 192.168.1.5 1 192.168.1.6 1 192.168.1.10 1 192.168.1.11 1 192.168.1.12 1 ); my %check; for my $ip ( keys %hashIPs ) { next unless $ip =~ /^(\d+\.\d+\.\d+)\.(\d+)$/; $check{ $1 } = 0 x 256 unless exists $check{ $1 }; substr $check{ $1 }, $2, 1, 1; } for my $key ( keys %check ) { print "Missing: $key.$-[0] - $key.", $+[0] - 1, "\n" while $check{ $key } =~ /(?<=1)0+(?=1)|^0+|0+$/g; } ' Missing: 192.168.1.3 - 192.168.1.3 Missing: 192.168.1.7 - 192.168.1.9 Missing: 192.168.1.13 - 192.168.1.255 #### my @segments = grep {/^(\d{1,3}\.\d{1,3}\.\d{1,3})/} keys %hashIPs; #### my @segments = map /^(\d{1,3}\.\d{1,3}\.\d{1,3})/, keys %hashIPs;