in reply to Quickest way to get the oldest file

my ($oldest) = map { $_->[0] } sort { $b->[1] <=> $a->[1] } map { [ $_, -M $_ ] } <*>;

The above isn't very memory-efficient, but is probably as quick as you can hope for. Change -M with -A or -C if necessary (see relevent Perl doc). Might also want to check that the file is not a directory. The <*> might have to be changed to get a directory other than the current one.

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

: () { :|:& };:

Note: All code is untested, unless otherwise stated

Replies are listed 'Best First'.
Re:x2 Quickest way to get the oldest file (don't sort)
by grinder (Bishop) on Jan 20, 2004 at 08:24 UTC

    Not only is this not memory-efficient, it's not speed-efficient either. There's no need to sort the entire list just to find the largest value. A reasonably efficient algorithm will require n*log(n) comparisons. With 50000 elements, the log(n) starts to become non-negligeable.

    Other suggestions here that involve walking down the list and noting the largest value are much more efficient both in terms of speed and memory. They require n comparisons and negligeable additional memory. It's a win-win situation.