Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
After some discussion with merlyn on IRC, I came up with the following.
use Socket; my $str = ""; while(<>) { chomp; my ($ip,$bits) = m,^(\d+(?:\.\d+){3})/(\d+)$, or next; $str .= ";" . unpack("B*",inet_aton($ip)); substr($str,-$bits) = "#" x $bits if $bits = 32 - $bits; } $str .= ";"; 1 while $str =~ s/;([01]+)0(#*);\1.\2;/;${1}#${2};/g; my @blocks = $str =~ /[10#]+/g; foreach my $block (@blocks) { my $bits = 32 - ($block =~ tr/#/0/); print inet_ntoa(pack("B*", $block)),"/$bits\n"; }

Which actually, I think, shows some errors in 3 .out files on your site

--- nm3b.out Thu Oct 11 21:54:06 2001 +++ nm3b.xx Sat Oct 13 00:08:51 2001 @@ -1,4 +1,3 @@ -130.91.7.0/31 -130.91.7.2/31 +130.91.7.0/30 255.255.255.254/31 255.255.255.255/32 --- nm7.out Thu Oct 11 21:54:07 2001 +++ nm7.xx Sat Oct 13 00:09:10 2001 @@ -1,4 +1,3 @@ -1.2.3.0/28 -1.2.3.16/28 +1.2.3.0/27 6.6.6.0/27 6.6.7.0/26 --- nm8.out Thu Oct 11 21:54:07 2001 +++ nm8.xx Sat Oct 13 00:09:10 2001 @@ -1,5 +1,4 @@ -1.2.3.0/28 -1.2.3.16/28 -5.4.3.1/31 +1.2.3.0/27 +5.4.3.0/31 6.6.6.0/27 6.6.7.0/26

Update:

That code does not handle the following case

101.33.45.27/30 101.33.34.0/24

use Socket; my @blocks; while(<DATA>) { chomp; my ($ip,$bits) = m,^(\d+(?:\.\d+){3})/(\d+)$, or next; push @blocks, unpack("B*",inet_aton($ip)); substr($blocks[-1],-$bits) = "#" x $bits if $bits = 32 - $bits; } my $str = ";" . join(";", sort @blocks) . ";"; 1 while $str =~ s/;([01]+)(#*);\1[01#]*;/;${1}${2};/g; 1 while $str =~ s/;([01]+)0(#*);\1.\2;/;${1}#${2};/g; my @blocks = $str =~ /[10#]+/g; foreach my $block (@blocks) { my $bits = 32 - ($block =~ tr/#/0/); print inet_ntoa(pack("B*", $block)),"/$bits\n"; }

In reply to Re: Challenge Problem: Merging Network Addresses by gbarr
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 cooling their heels in the Monastery: (3)
As of 2024-04-16 15:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found