I have a list of IP addresses that I am trying to sort. For example,

123.456.789.12 12.345.67.19 12.345.67.18 12.345.66.20

I am trying to get a Perl script that will sort these in terms of each set of numbers from smallest to largest. I.e., the first set of numbers before the first period are used to sort. Then, if there are matches, the second set of numbers are compared and sorted. And so on. So, in the above case, the sort would result in:

12.345.66.20 12.345.67.18 12.345.67.19 123.456.789.12

I tried the following:

#!/usr/bin/perl open (FILE,"ipsvisiting.txt"); while (<FILE>) { $j[$i++] = $_; } close FILE; sub nummer { $a <=> $b; } @sorted = sort nummer @j; foreach $s (@sorted) { print $s; }

This "seems" to do some sorting, but it results in things like this:

12.345.111.3 12.345.111.26 12.345.111.24 12.345.111.5 12.345.111.4

In other words, it seems to stop working after it reaches the last set of numbers.

Any idea how to fix this?


In reply to How to sort IP addresses by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.