The only other way I can think of (others may know better) would be to parse the output from ls or dir with the appropriate sort options. but that is much harder than using a hash to control a sort.
#! perl -sw use strict; my %times; opendir DIR, $ARGV[0] or die "Couldn't open $ARGV[0]; $!\n"; my @sorted = sort { $times{$b} <=> $times{$a}; # Was cmp D'oh! Thanks [dws] } map { $times{$_} = (stat $_)[9]; $_; } grep{ -f $_ } readdir DIR; closedir DIR or warn "Couldn't close $ARGV[0]; $!\n"; local $" = $/; print "@sorted\n";
You might need to change the stat index from 9 to either 8 or 10 depending upon your needs.
I also think that testing using -f is safer than using your match to rid yourself of '.' and '..', though if your on a system that supports and uses symbolic links for example, you might need to bolster the grep test.
Oh! And if you want them oldest first, switch the order of $a and $b of course.
Updated to correct cmp -v- <=> error. Thanks dws.
In reply to Re: READDIR - Sort by Time?
by BrowserUk
in thread READDIR - Sort by Time?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |