Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Better late than never, eh? This uses a hash to store all the ranges, has to do an explicit sort and so doesn't depend on the input being sorted. It runs in about 10 seconds on your nm5.in sample file (which is a good deal better than the 3 1/2 minutes it took me to run Net::CIDR and end up with non-optimal results). I haven't tried real hard to break it, and it doesn't check for valid data, so buyer beware.

There was a similar (generic) range merging script posted awhile ago here on PM that used a hash and an accumulating count, I can't find it at the moment or remember who posted it, but credit to them, whoever they may be :)

Updated with tye's suggestion from this node.

use strict; use warnings; use Socket qw(inet_ntoa inet_aton); my @masks = map { pack("B*", substr("1" x $_ . "0" x 32, 0, 32)) } 0..32; my @bits2rng = map { 2**(32 - $_) } 0..32; my %rng2bits = map { $bits2rng[$_] => $_ } 0..32; my %ips; while (<>) { my ($ip, $mask) = split "/"; my $start = inet_aton($ip) & $masks[$mask]; my $end = pack("N", unpack("N", $start) + $bits2rng[$mask]); $ips{$start}++; $ips{$end}--; } my ($start, $total); for my $ip (sort keys %ips) { $start = $ip unless $total; unless ($total+=$ips{$ip}) { my $diff = unpack("N", $ip) - unpack("N", $start); while ($diff) { (my $zeros = unpack("B*", $start)) =~ s/^.*1//; my $range; for my $i (32-length($zeros)..32) { $range=$bits2rng[$i], last if $bits2rng[$i]<=$diff; } print inet_ntoa($start), "/", $rng2bits{$range}, "\n"; $start = pack("N", unpack("N", $start)+$range); $diff -= $range; } } }
Update: See also Net::CIDR::Lite ?? (Merge CIDR addresses).

In reply to Re: Challenge Problem: Merging Network Addresses by runrig
in thread Challenge Problem: Merging Network Addresses by Dominus

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-26 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found