On Fri, Jul 24, 2009 at 00:18, Joseph L.
Casale<[email protected]> wrote:
> Hi,
> I have some data I will read into an array that is the format
> "some_string"@"date" such as "foo/bar/[email protected]".
>
> 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/[email protected]
foo/bar/[email protected]
[email protected]
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: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/