Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: sorting entires by date

by Aristotle (Chancellor)
on Jan 01, 2004 at 23:04 UTC ( #318181=note: print w/ replies, xml ) Need Help??


in reply to sorting entires by date

Basically you want a Schwartzian Transform. You parse the lines into a list of lists, sort it by the desired field, then extract the original data.

my @sorted = map $_->[0] sort { $a->[4] <=> $b->[4] } map [ $_, split /:/ ], <>;
Of course if you want to keep them around in the parsed form for later steps, you can leave out the copy of the line and the extraction step:
my @sorted = sort { $a->[4] <=> $b->[4] } map [ split /:/ ], <>;

I am too lazy to explain the Schwartzian transform all over again :), so I Super Searched for some explaination. Surprisingly I came up empty for the time being..

The probably most important to understand parts are references and how to use them to create complex data structures in Perl. If you understand that, the Transform itself should be almost self-explanatory. Check out perldoc perlreftut, perldoc perldsc, and perldoc perllol.

Makeshifts last the longest.


Comment on Re: sorting entires by date
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://318181]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2014-07-12 09:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (239 votes), past polls