in reply to Sorting a textfile by a date field
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Sorting a textfile by a date field
by simonm (Vicar) on Oct 01, 2003 at 16:55 UTC |