http://www.perlmonks.org?node_id=118595


in reply to Challenge Problem: Merging Network Addresses

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"; }