Monks, let me start with the code:
sub sort_by_ip { { no warnings; @sorted_by_ip_date_time = sort { my @a = $a->[0] =~ split/./; #split IP address on periods my @b = $b->[0] =~ split/./; #split IP address on periods my @c = $a->[3] =~ split/\//; #split date on forward slashes my @d = $b->[3] =~ split/\//; #format is '27/Jun/2001' my @e = $a->[4] =~ split/:/; #split time on colons my @f = $b->[4] =~ split/:/; #format is '02:23:15' if ($c[1] == 'Jun') { ##convert the months into numbers (on +ly two months in log)## $c[1] = 6; #6 if 'Jun' } else { $c[1] = 7; } #7 if 'Jul' if ($d[1] == 'Jun') { $d[1] = 6; #6 if 'Jun' } else { $d[1] = 7; } #7 if 'Jul' $a[0] <=> $b[0] || #sort by 1st part of IP else if same... $a[1] <=> $b[1] || #sort by 2nd part of IP else if same... $a[2] <=> $b[2] || #sort by 3rd part of IP else if same... $a[3] <=> $b[3] || #sort by 4th part of IP else if same... $c[2] <=> $d[2] || #sort by year else if same... $c[1] <=> $d[1] || #sort by month, 6 (June) or 7 (July) only else + if same... $c[0] <=> $d[0] || #sort by day else if same... $e[0] <=> $f[0] || #sort by hour else if same... $e[1] <=> $f[1] || #sort by minute else if same... $e[2] <=> $f[2] #sort by second (phew!) } @logentry; } }
I'm trying to sort an array (@logentry) of an anonymous array on 3 elements of the anonymous array in this order: IP (element 0), Date (element 3), and Time (element 4). Hopefully my comments make everything else abundantly clear.

I don't get any errors and it runs quickly. However, when I print out the results, the @sorted_by_ip_date_time array is not sorted in any recognizeable order. It looks almost completely random. It's doing something because the order is different than the original log. I've spent an hour and a half on this. I've never attempted such a complex sort; please tell me the error of my ways.

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop";
$nysus = $PM . $MCF;
Click here if you love Perl Monks


In reply to Hella sort by nysus

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.