logo       

Re: Question on approach: msg#00345

perl-beginners

Subject: Re: Question on approach

On Fri, Jul 24, 2009 at 00:18, Joseph L.
Casale<JCasale@xxxxxxxxxxxxxxxxx> wrote:
> Hi,
> I have some data I will read into an array that is the format
> "some_string"@"date" such as "foo/bar/biz@xxxxxxxxxxxxxxxxxxx".
>
> 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/biz@xxxxxxxxxxxxxxxxxxx
foo/bar/baz@xxxxxxxxxxxxxxxxxxx
quux@xxxxxxxxxxxxxxxxxxx

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


Google Custom Search

News | Mail Home | sitemap | FAQ | advertise