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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.