http://qs321.pair.com?node_id=598570


in reply to sorting files by date

One thing that everybody here is forgetting is that unless you are doing a readdir on the current directory, you must prepend the directory name for -M, -C, stat, etc.

Here are some ways to do it:

#!perl -w use strict; use File::stat; # Method 1: my $input_directory_lowest = "pridir"; opendir (LOWEST, $input_directory_lowest) or die "Error opening directory: $!"; my @files; while (my $fname = readdir LOWEST) { if ($fname =~ /^Flat_file.*\.txt$/) { push @files, [stat("$input_directory_lowest/$fname")->ctime, $fname]; } } closedir(LOWEST); my @low1 = map { $_->[1] } sort {$a->[0] <=> $b->[0]} @files; print join("\n ", "low1=", @low1), "\n"; # Method 2: same as 1, but without explicit loop opendir (LOWEST, $input_directory_lowest) or die "Error opening directory: $!"; my @low2 = map { $_->[1] } sort {$a->[0] <=> $b->[0]} map { [ stat("$input_directory_lowest/$_")->ctime, $_ ] } grep /^Flat_file.*txt$/, readdir(LOWEST); closedir(LOWEST); print join("\n ", "low2=", @low2), "\n"; # Method 3: outputs file names with directory; uses 'glob' my @low3 = map { $_->[1] } sort {$a->[0] <=> $b->[0]} map { [ stat($_)->ctime, $_ ] } glob("$input_directory_lowest/Flat_file*.txt"); print join("\n ", "low3=", @low3), "\n";