sub byip { inet_ntoa($a) cmp inet_ntoa($b) } foreach (sort byip @ip_list) ...