in reply to How to compare time in perl

You don't have to parse the dates. Since the strings are a) constant width and consistently formatted, and b) organized left-to-right in order of granularity, you can simply use a string comparison. Like so:

#!/usr/bin/perl @data = <DATA>; chomp @data; foreach (sort { $a cmp $b } @data) { print "$_\n"; } __END__ 2003/02/05 17:32:06.179 2003/02/05 17:32:06.159 2003/01/05 17:32:06.179 1999/01/05 17:32:06.179
When run this prints:
1999/01/05 17:32:06.179 2003/01/05 17:32:06.179 2003/02/05 17:32:06.159 2003/02/05 17:32:06.179

Matt

Replies are listed 'Best First'.
Re: Re: How to compare time in perl
by tachyon (Chancellor) on Feb 08, 2003 at 19:50 UTC

    The equal width and 0 packing of single digits is vital as this will choke cmp:

    2003/02/05 2:32:06.179 2003/02/05 17:32:06.179 2003/02/05 20:32:06.159
    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Even if they aren't to begin with he can normalize them first:
      $time=~s{ #$1 $2 $3 $4 $5 $6 $7 (\d+)/(\d+)/(\d+)\s+(\d+):(\d+):(\d+)\.(\d+) } { sprintf '%04d/%02d/%02d %02d:%02d:%02d.%3d', $1, $2, $3, $4, $5, $6, $7 }xe or die "Failed to normalize '$time'\n";

      --- demerphq
      my friends call me, usually because I'm late....