On Fri, Jul 24, 2009 at 00:18, Joseph L.
Casale<jcas...@activenetwerx.com> wrote:
> Hi,
> I have some data I will read into an array that is the format
> "some_string"@"date" such as "foo/bar/b...@07-23-2009-11.42.02".
>
> To work with this, I will convert the date part (everything after
> the "@" to the epoch format so it's easy to sort accurately. I
> figured I would create a hash with keys being the original array
> value and corresponding epoch format of the date section of each
> array member.
>
> I was going to perform a sort, then start shifting values off the
> array of hash values while doing certain checks for other stuff.
> This all seemed like a good approach but it seems it's rather
> tedious to then lookup the hash key from the shifted array value.
>
> Anyone got a suggestion on a smarter approach to this? I am only a
> hack at Perl and don't mind reading how to execute it but I am at a
> loss for a smarter way to perform this rather trivial task!
snip

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:

#!/usr/bin/perl

use strict;
use warnings;

for my $item (sort { $a->[1] cmp $b->[1] } map { [ split /[...@\n]/ ] } <DATA>) 
{
        my ($path, $date) = @$item;
        print "path $path date $date\n";
}

__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

I say probably because you do not want to do this if the file is large
in comparison to the amount of RAM you have available.

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
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