Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
If you want to sort a file by such a method, it would be best to read the file into an array (angle operator or Tie::File) and then use the Schwartzian Transform (read about it here) like this:
@file = map { $$_[0] } sort { $$a[1] <=> $$b[1] } map { [$_, (split /:/)[3] ] } @file;
Hope that helps!

Thought I'd make the ST clearer. Basically, you start with a map statement which takes every line in your file and puts it in an anonymous array with the timestamp (via split). Then, it passes an array with each of these to the sort routine, which sorts them based on the timestamp by getting that info from each anonymouss array in turn. Finally, the map statment at the end transforms this array of anonymous arrays back into an array with the lines from the file, properly sorted. Tada!

You could also try a GRT, which might look something like this:
@file = map { join ':', (split /:/)[1,2,3,0] } sort map { join ':', (split /:/)[3,0,1,2] } @file;
This has the clear limitation that it sorts asciibetically rather than numerically (doing it numerically might mitigate the benifits of GRT over ST, I'm not sure), which shouldn't be a problem really with time() but might be something to watch out for. Also, I'm sure this should have been done with pack or some such nonsense, but I don't know how. And finally, it may not even be faster. But, TMTOWTDI.

Who is Kayser Söze?
Code is (almost) always untested.

In reply to Re: sorting entires by date by jweed
in thread sorting entires by date by Anonymous Monk

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!
  • 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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (5)
    As of 2015-10-04 11:27 GMT
    Find Nodes?
      Voting Booth?

      Does Humor Belong in Programming?

      Results (101 votes), past polls