The route I recently went that worked well for the mix of IPv4 addresses and ranges that I had was to, for IPv4 addresses, just use the IP address as a hash key. For IP ranges, I used the first 3 octets of the range as the hash key and the value was a list of ranges that I looped through (it was rare that I had more than one range having the same first three octets). I didn't have any ranges that covered more than one three-octet block, but if I had, I would split such ranges up.

my %hash= ( '127.0.0.1' => 'localhost', '10.11.12.1' => 'router', '10.11.12.10' => 'desktop', '10.11.12.' => [ { min => 101, max => 120, desc => 'printers' }, { min => 250, max => 254, desc => 'tanks' }, ], ); sub findIP { my( $ip )= @_; my $desc= $hash{$ip}; return( $ip, $desc ) if $desc; my( $last )= $ip =~ s/(\d+)$//; my $ranges= $hash{$ip}; for my $range ( @{ $ranges || [] } ) { my( $min, $max )= @$range{'min','max'}; return( "$ip.$min-$max", $range->{desc} ) if $min <= $last && $last <= $max; } return; }

- tye        


In reply to Re: Quickly determine which IP range an IP belongs to. (two keys) by tye
in thread Quickly determine which IP range an IP belongs to. by punch_card_don

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.