Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2014-09-30 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (385 votes), past polls