in reply to Reading from ls-lrt (was: Reading)

Gave me a chance to learn opendir and map :-)

opendir LSLRT, "$dir" or die "I hate you, $dir: $!"; # rewinddir not needed @dir = readdir(LSLRT); closedir LSLRT; # why 'or die,' if we're all done anyways? for my $dirent (@dir) { next unless ( -f "$dir/$dirent" ); $mtime{$dirent} = (stat "$dir/$dirent")[9]; } my @last_five = (map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [$_, $mtime{$_} ] } keys %mtime)[-5..-1];

This is evil because I sort the whole thing and then throw away most of it.

For easy-to-maintain:

my @last_five = chomp (`ls -1 --sort=time`)[0..4];

Corrections: Don't need to reverse in the ls -1 form, and don't need to rewinndir.

N.B. that I use -1 because CygWin's ls will send ANSI colour codes if I leave it off :-(

MORE Corrections: Stole chipmunk and gryng and kschwab's ideas from CB. I like the below much better than the prior attempt above.

opendir LSLRT, "$dir" or die "I hate you, $dir: $!"; my @files = (map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { ( -f and /^F/ ) ? [$_, (lstat $_)[9] ] : () } readdir LSLRT); closedir LSLRT; @files = @files[0..($#files > 3 ? 4 : $#files)];