Even at 250 lines of 512 bytes each (pretty long lines) you're only looking at 128k worth of data, so you don't really need to worry about trying to tie an array to a file or something like that. You can do the most time-efficient / programming-efficient thing, and just slurp it in. ....slurping a file in in its entirety isn't all that great usually, except when you're sorting, in which case it really can simplify your life.
If you're sure that the last field always contains the date, and you want to sort by date, you can probably do something like this:
use strict;
use warnings;
use Date::Manip;
my @array = <DATA>; # Slurp in the whole file, line by line.
@array = map { [split /\t/, $_] } @array;
@array = sort{ParseDate($a->[-1]) cmp ParseDate($b->[-1])} @array;
@array = map { join "\t", @{$_} } @array;
Let me explain that....
The first line slurps in your whole file into @array.
The second line turns each element of @array into an anonymous array where each element is one field from a line of your file. The last field is the date.
The third line takes advantage of Date::Manip's ability to turn just about any string into a date that can be compared with cmp. That line also uses a sort routine where you've defined the comparison mechanism to compare the last element of the anonymous array contained in each line of @array.
The fourth (last) line joins up the elements of the anonymous array contained in each line of @array, so that each line now contains its original tab delimited version.
If you want to express that in fewer lines:
@array = map { join "\t", @{$_} }
sort{ParseDate($a->[-1]) cmp ParseDate($b->[-1])}
map { [split /\t/, $_] } @array;
The above is untested, so you may have to tinker a little to get it to your liking. You may even prefer to use a different module (a standard library one, for example). But the logic demonstrated ought to be a pretty good starting point.
Good luck. I hope this helps!
Dave
"If I had my life to do over again, I'd be a plumber." -- Albert Einstein
|