Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

GRT sort of files by time

by blazar (Canon)
on Jul 11, 2007 at 22:32 UTC ( [id://626100]=CUFP: print w/replies, xml ) Need Help??

Minimal example of Guttman-Rosler Transform applied to sorting files according to (m)time. (Some more details in reply.)
my @sorted=map substr($_,4), sort map pack(N => (stat)[9]) . $_, @files;

Replies are listed 'Best First'.
Re: GRT sort of files by time
by blazar (Canon) on Jul 11, 2007 at 22:54 UTC
    my @sorted=map substr($_,4), sort map pack(N => (stat)[9]) . $_, @files;

    Initially, to allow the maximum freedom on the actual size of an integer to hold a time specification, I would have liked to use the 'I' pack template as follows:

    my $len=length pack I => 1; my @sorted=map substr($_,$len), sort map pack(I => (stat)[9]) . $_, @files;

    Unfortunately, these were not suitable for sorting ("at least" on my machine) due to little-endianness. Appearently there are no templates for "an unsigned integer of the same bit width as 'l' ones (at least 32) and with prescribed endiannes." I have been thinking about reverse, but that would make the code too clumsy too.

    On a much simpler basis, I've also considered sprintf, but then if we look after GRT, it may mean we're concerned about efficiency. (Although in some cases like this one it can provide a very clean and terse solution too.) In which case I've run the following benchmark:

    (tested in a directory with ~1000 files) which gives me the following results:

    Update: correct Benchmark results as per salva's remark. Funnily enough, the figures as far as percentages are concerned are not that different from the previous ones.

    Rate sprf2 sprf1 spack sprf2 319/s -- -6% -21% sprf1 340/s 6% -- -16% spack 403/s 26% 19% --

    YMMV

      your benchmarking code is wrong:
      ... my @files=glob '*'; ... cmpthese -30, { map { $_ => "$_(\@files)" } @sub };
      @files is a lexical not available from outside its scope. You are actually sorting nothing.

      You can solve that bug using a global variable for @files:

      ... our @files=glob '*'; ... cmpthese -30, { map { $_ => "$_(\@main::files)" } @sub };

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 17:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found