in reply to Sort directories by date
But maybe those two possible interpretations happen to yield the same relative ordering of directories - that is, it could be that no changes ever occur in the contents of "09302015" once "10012015" is created (and likewise for the latter, once "10022015" is created). If this is true, you can sort the directories based on their modification dates:
Of course, if you really, truly meant for the actual directory names to be the basis for sorting, then the Schwartzian Transform is probably the most economical: instead of using the hash and while loop shown above, just do this:my $dir = "/my_dir"; opendir DIR, $dir or die "$dir: $!\n"; my %subdir; while ( my $month_dir = readdir( DIR ) { next unless ( $month_dir =~ /^\d{8}$/ ); $subdir{$month_dir} = -M $month_dir; } my $latest_dir = ( sort {$subdir{$a}<=>$subdir{$b}} keys %subdir )[0]; # ...
my $latest_dir = ( map{s/(....)(....)/$2$1/; $_} sort map{s/(....)(....)/$2$1/; $_} grep /^\d{8}$/, readdir DIR )[-1];
|
|---|