logo       

Re: Question on approach: msg#00352

perl-beginners

Subject: Re: Question on approach

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/biz@xxxxxxxxxxxxxxxxxxx
foo/bar/baz@xxxxxxxxxxxxxxxxxxx
quux@xxxxxxxxxxxxxxxxxxx
/some/path@xxxxxxxxxxxxxxxxxxx

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-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
http://learn.perl.org/


Google Custom Search

News | Mail Home | sitemap | FAQ | advertise