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.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 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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |