Shawn H. Corey wrote:
Chas. Owens wrote:
That date format is directly sortable, so unless you have another
reason to convert to epoch time just use a string comparison in the
sort.  I would probably write the code like this:

Unfortunately, the data is not directly sortable since the date is in American format, not Système International (SI). SI dates are directly sortable and are the preferred format for storing dates.

I would use a heap to sort:

#!/usr/bin/perl

use strict;
use warnings;

my %data = ();

while( <DATA> ){
  my ( $path, $am_date ) = split m{ \@ }msx, $_, 2;

I usually like simplicity:   split /\@/, $_, 2


  my ( $mo, $day, $yr, $time ) = split m{ \- }msx, $am_date;

                                   split /-/, $am_date


  $data{$yr}{$mo}{$day}{$time} = $_;
}
print Dumper \%data;

for my $yr ( sort { $a <=> $b } keys %data ){
  for my $mo ( sort { $a <=> $b } keys %{ $data{$yr} } ){
    for my $day ( sort { $a <=> $b } keys %{ $data{$yr}{$mo} } ){
      for my $time ( sort { $a cmp $b } keys %{ $data{$yr}{$mo}{$day} } ){
        print $data{$yr}{$mo}{$day}{$time};
      }
    }
  }
}

__DATA__
foo/bar/b...@07-23-2009-11.42.02
foo/bar/b...@07-22-2009-10.00.00
q...@07-24-2009-23.59.00
/some/p...@06-10-2009-12.30.13

When I run your code I get:

Name "main::Dumper" used only once: possible typo at Question_on_approach.pl line 13. print() on unopened filehandle Dumper at Question_on_approach.pl line 13, <DATA> line 4.


Or you could use an ST instead:

#!/usr/bin/perl
use strict;
use warnings;

print
    map  $_->[ 0 ],
    sort { $a->[ 1 ] cmp $b->[ 1 ] }
map [ $_, join '', ( /^([...@]+)\@(\d+)-(\d+)-(\d+)-(.+)/ )[ 3, 1, 2, 4, 0 ] ],
    <DATA>;


Or a GRT:

#!/usr/bin/perl
use strict;
use warnings;

print
    map  /\0([^\0]+)/,
    sort
map join( '', ( /^([...@]+)\@(\d+)-(\d+)-(\d+)-(.+)/ )[ 3, 1, 2, 4, 0 ] ) . "\0$_",
    <DATA>;





John
--
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to