Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: How can I sort this array?

by flounder99 (Friar)
on Jun 17, 2002 at 19:24 UTC ( #175172=note: print w/ replies, xml ) Need Help??

in reply to How can I sort this array?

Time::Local is core and faster than Date::* functions

use Time::Local qw( timelocal ); use strict; my @input = reverse qw( Order_29May11:05.bak Order_11Jun20:25.bak Order_10Jun14:16.bak Order_10Jun16:04.bak ); # reversed to prove it works since they are already sorted my @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); my %months; for (0 .. $#months) {$months{$months[$_]} = $_} # merlyn's definition of the # Schwartzian Transform # changed to numerical sort my @output = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [$_, expensive_func($_)] } @input; print join "\n", @output; exit; sub expensive_func { # convert filename to date $_[0] =~ /Order_(\d+)(...)(\d+):(\d+)\.bak/ or die "bad filename $_[0]"; return timelocal(0, $4, $3, $1, $months{$2}, 0); } __OUTPUT___ Order_29May11:05.bak Order_10Jun14:16.bak Order_10Jun16:04.bak Order_11Jun20:25.bak
-- flounder

Comment on Re: How can I sort this array?
Download Code

Log In?

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

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

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (754 votes), past polls