John W. Krahn wrote:
Amit Saxena wrote:
my @sorted_files_in_directory;
@sorted_files_in_directory = sort { (stat($a))[9]<=> (stat($b))[9] }
If you read the documentation for readdir you will see where it says:
If you're planning to filetest the return values out of a
"readdir", you'd better prepend the directory in question.
Otherwise, because we didn't "chdir" there, it would have
been testing the wrong file.
So with the directory prepended you would have:
my @sorted_files_in_directory = sort { (stat "$directory_name/$a" )[ 9 ]
<=> ( stat "$directory_name/$b" )[ 9 ] } @files_in_directory;
Although that means that you are stat-ing each file more than once. A
more efficient way to do that is to use a Schwartzian Transform which
will only stat each file once:
my @sorted_files_in_directory =
map $_->[ 1 ],
sort { $a->[ 0 ] <=> $b->[ 0 ] }
map { stat "$directory_name/$_", $_ }
@files_in_directory;
Correction:
my @sorted_files_in_directory =
map $_->[ 1 ],
sort { $a->[ 0 ] <=> $b->[ 0 ] }
map { ( stat "$directory_name/$_" )[ 9 ], $_ }
@files_in_directory;
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/