my $fp = \&by_ip; print $fp->(@input); sub by_ip { return map { unpack('x4A*') } sort map { pack('A4A*',inet_aton((split /\s/,$_,2)[0]),$_) } @_; }