in reply to How do I sort a file with IP Addresses and another text column?
The inet_aton function returns four bytes, the value of the IP address in network order, which sorts properly using an ASCII sort. The Socket module is standard with Perl.use Socket qw [inet_aton]; sub squish { my($ip_lo,$ip_hi,$name) = split (//,$_[0]); return $name.inet_aton($ip_lo).inet_aton($ip_hi); } @sorted = sort { squish($a) cmp squish($b) } @unsorted;
|
|---|