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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |