What I'd do is simply use DB_File like so (untested, but hey, I wrote it ;))
#!/usr/bin/perl -w use strict; use Fcntl; use DB_File; use Date::Calc qw|Date_to_Time Decode_Month|; my $filename = __FILE__.".${$}.db"; # $DB_BTREE is exported by DB_File # it is a sorted balanced binary tree # we enable duplicate keys $DB_BTREE->{'flags'} = R_DUP ; # since default sorting is lexical, and we want numeric $DB_BTREE->{'compare'} = sub { my( $keyA, $keyB ) = @_; return $keyA <=> $keyB; }; my $X = tie my(%H), "DB_File", $filename, O_RDWR|O_CREAT|O_TRUNC, 0666, $DB_BTREE or die "Cannot open $filename: $!\n"; foreach $file (@ARGV) { die("This is not a file: $!") unless (-f $file); open(FILE, $file) or die "WTF? couldn't open $file cause $!"; while(<FILE>){ chomp; my $date = get_timestamp($_); $H{$date} = $_; } close(FILE); } # iterate through the btree using seq # and print each key/value pair. my $key = 0; my $value = 0 ; my $status = ""; for( $status = $x->seq($key, $value, R_FIRST) ; $status == 0 ; $status = $x->seq($key, $value, R_NEXT) ) { print "$value\n"; } undef $X ; untie %H ; # delete our temporary database unlink $filename or die "couldn't delete $filename cause $!"; exit; ## subland sub get_timestamp { my $line = shift; $line =~ /\[(.*) -\d+\]/; my $tempdate = $1; my ($day,$mon,$year,$hour,$min,$sec) = $tempdate =~ /(\d+)\/(\w+)\/(\d+):(\d+):(\d+):(\d+)/; $mon = Decode_Month($mon); my $stamp = Date_to_Time($year,$mon,$day,$hour,$min,$sec); }
I personally wouldn't bother with all that unneccessary time consuming Date::Calc nonsense, and if you do that, you wouldn't have to bother with the custom compare routine.

On another note, your get_timestamp sub has a classic logic flaw, you assign $1 to something, without ever knowing if there was a match. You should if(/(match)){ $foo = $1; ... }

____________________________________________________
** The Third rule of perl club is a statement of fact: pod is sexy.


In reply to Re: Sorting apache log files by PodMaster
in thread Sorting apache log files by rlb3

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.