in reply to How to sort IP addresses

inet_aton returns the address as a 4 byte string. Each byte of the string represents one of the numbers of the IP address. In this format, they can be sorted lexically.
use Socket qw( inet_aton ); use List::MoreUtils qw( apply ); my $file_name = "ipsvisiting.txt"; open (my $fh, '<', $file_name) or die("Unable to open file \"$file_name\": $!\n"); my @sorted = map { substr($_, 4) } sort map { inet_aton($_) . $_ } apply { chomp } <$fh>; print "$_\n" foreach @sorted;

I started writing this when there were no replies, but I got sidetracked. Hopefully, this approach hasn't been suggested already.

I made some change to your code: