in reply to How to sort IP addresses
Then it's just a matter of doing a straightsub ipto32bit { # # Convert a dotted quad c.d.e.f to a single unsigned 32bit number. # my ($ip, $c, $d, $e, $f); $ip = shift; ($c,$d,$e,$f) = split(/\./,$ip); return ($c << 24) + ($d << 16) + ($e << 8) + $f; }
Which gives:#!/usr/bin/perl -wl use strict; chomp(my @ips = <DATA>); my @sorted_ips = sort by_ip(@ips); print for (@sorted_ips); sub by_ip { return ipto32bit($a) <=> ipto32bit($b); } sub ipto32bit { # # Convert a dotted quad c.d.e.f to a single unsigned 32bit number. # my ($ip, $c, $d, $e, $f); $ip = shift; ($c,$d,$e,$f) = split(/\./,$ip); return ($c << 24) + ($d << 16) + ($e << 8) + $f; } __DATA__ 12.345.111.3 12.345.111.26 61.8.47.111 12.345.111.24 203.134.35.27 12.345.111.5 12.345.111.4
12.345.111.3 12.345.111.4 12.345.111.5 12.345.111.24 12.345.111.26 61.8.47.111 203.134.35.27
Note that I started using the above before I learned about the inet_aton goodness that was pointed out by shmem above. So if I was looking for a solution now, shmem's is probably the one I would choose.
Cheers,
Darren :)
|
|---|