in reply to How can I access a cross directory set of data files by most recently modified date?

From the CB:

In my case I may not need to sort the entire list. I need to get the most recent file, then I may need the next most recent file, and perhaps the next one after that.

If most of the time, the latest satisfies your need, you could do a linear search for it. Then do something more expensive a real sort if you need further info.

my $newest_time; my @newest_files; my %times; foreach my $file (@files) { my $time = (stat($file))[9]; $times{$file} = $time; if ($time > $newest_time) { $newest_time = $time; @newest_files = $file; } elsif ($time == $newest_time) { push @newest_files, $file; } } foreach my $file (@newest_files) { if ( ...[ this is the file we want ]... ) { return $file; } } @files = sort { $times{$b} <=> $times{$a} } @files; # Remove the ones we already checked. splice(@files, 0, scalar(@newest_files)); foreach my $file (@files) { if ( ...[ this is the file we want ]... ) { return $file; } }

You might want to benchmark against sorting the whole list.

my %times; $times{$_} = (stat($file))[9] foreach @files; @files = sort { $times{$b} <=> $times{$a} } @files;

Update: Fixed problems raised by shmem.

  • Comment on Re: How can I access a cross directory set of data files by most recently modified date?
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: How can I access a cross directory set of data files by most recently modified date?
by shmem (Chancellor) on Jun 07, 2007 at 18:49 UTC

    ikegami,

    seems like you're slacking a bit.

    my $time = -d $file;

    Huh? directory test to get the time? or is it that the variable $time is spectacularily ill-chosen?

    if ($time > $newest_time) { $newest = $time;

    use strict; use warnings... :-)

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}