in reply to Sorting subnets into a tree

I don't know of any existing routine, but I think something like this should work:
use Net::Netmask; my @blocks = (...); # list of your cidr blocks my @sorted = sort { ($a->bits <=> $b->bits) || ($a->base <=> $b->base) + } @blocks; my @children; my @parent; for my $i (0..$#sorted) { for (my $j = $i-1; $j >= 0; $j--) { if ($sorted[$j]->contains($sorted[$i])) { $parent[$i] = $j; # or = $sorted[$j]; push(@{$children[$j]}, $i); # or $sorted[$i] last; } } }
If you want the whole tree rooted, just add:
unshift(@sorted, new Net::Netmask("0.0.0.0/0")); # one subnet to rule +them all

Replies are listed 'Best First'.
Re^2: Sorting subnets into a tree
by C_T (Scribe) on Mar 19, 2008 at 19:09 UTC
    This is very much along the lines of what I was thinking of doing by "roll my own". Definitely wanted to make sure I wasn't missing some existing Perl module that would do it automagically. Thanks very much! CT
    Charles Thomas
    Madison, WI