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


In reply to Re: Sorting a textfile by a date field by davido
in thread Sorting a textfile by a date field by bionicle32

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.