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.


In reply to Re: How can I access a cross directory set of data files by most recently modified date? by ikegami
in thread How can I access a cross directory set of data files by most recently modified date? by SkipHuffman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.