I'm having a hard time understanding what you are trying to do. I think you are trying to list every 24 bit subnet* whose 256 IP addresses are included in a given list of IP addresses (keys %hashIPs). Correct me if I'm wrong.
Since keys %hashIPs can't contain any duplicates, all you have to do is count how many addresses are in each subnet.
For starters, it's much easier to work with IP addresses as numbers so that numerical operators can be used. It's also easy to work on them in packed form, since most numerical operators will also work on those.
use strict; use warnings; sub pack_ipv4 { my $dotted_ip = @_ ? $_[0] : $_; return pack('C4', split(/\./, $dotted_ip)); } sub unpack_ipv4 { my $packed_ip = @_ ? $_[0] : $_; return join('.', unpack('C4', $packed_ip)); } sub get_subnet { my ($packed_ip, $subnet_size) = @_; my $packed_mask = pack('B32', ('1' x $subnet_size) . ('0' x (32-$su +bnet_size))); return $packed_ip & $packed_mask; } my %hashIPs = map { $_ => 1 } ( (map { "10.0.0.$_" } 0..255), (map { "10.0.1.$_" } 0..254), (map { "10.0.2.$_" } 0..255), ); my %count; for (keys %hashIPs) { ++$count{get_subnet(pack_ipv4($_), 24)}; } for (grep { $count{$_} == 256 } keys %count) { print(unpack_ipv4($_), "/24\n"); }
The bottom bit could also be written as
my %count; print("$_/24\n") for map unpack_ipv4, grep ++$count{$_} == 256, map get_subnet($_, 24), map pack_ipv4, keys %hashIPs;
* — There's really no such thing as subnet classes anymore. What used to be a "Class C subnet" is now a "24 bit subnet" or "subnet with mask 255.255.255.0". You're doing yourself a disservice by thinking in terms of classes.
In reply to Re: Matching Sequential IP Addresses
by ikegami
in thread Matching Sequential IP Addresses
by Dru
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |