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 |