|
|
| Pathologically Eclectic Rubbish Lister | |
| PerlMonks |
Re: Sorting a textfile by a date fieldby davido (Cardinal) |
| on Oct 01, 2003 at 02:07 UTC ( [id://295526]=note: print w/replies, xml ) | Need Help?? |
This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.
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:
Let me explain that.... 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:
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
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||||||