in reply to Sorting IP Subnets together
Remember, an IP address is simply a 32-bit number. The dotted form is just a representation of that number. While the dotted form might be easier to transcribe, it's easier to work with the 32-bit number directly when working with subnets.
The following actually uses the packed form of the 32-bit numbers. It's easier to obtain, and allows for easy sorting (sort vs sort { $a <=> $b }).
use strict; use warnings; my $subnet_size = 29; my $subnet_mask = pack('B*', ('1' x $subnet_size) . ('0' x (32-$subnet_size))); my @ip_xfers = ( [ '10.0.0.1', 555555 ], [ '10.2.1.6', 444444 ], [ '10.6.2.8', 333333 ], [ '10.2.1.2', 222222 ], ); my %subnet_xfers; foreach (@ip_xfers) { my ($ip, $xfer) = @$_; my $packed_ip = pack('C4', split(/\./, $ip)); my $subnet = $packed_ip & $subnet_mask; $subnet_xfers{$subnet} ||= 0; $subnet_xfers{$subnet} += $xfer; } foreach my $subnet (sort keys %subnet_xfers) { my $subnet_ip = join('.', unpack('C4', $subnet)); my $xfer = $subnet_xfers{$subnet}; print("$subnet_ip/$subnet_size: $xfer\n"); }
10.0.0.0/29: 555555 10.2.1.0/29: 666666 10.6.2.8/29: 333333
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sorting IP Subnets together
by ikegami (Patriarch) on Apr 12, 2007 at 16:41 UTC |