icarus wrote:
Thanks Rob and Martin for your inputs.

I found a solution to my problem.  I'm posting it here for whoever
might need it.  I'm sure there's a faster solution out there but this
will do just fine.
I had to put the files in an array. :(
The reason is that you cannot sort a scalar [ or scalar context such
as reading a dir using while (<MYDIR>) ] , it has to be an array.


use strict;
use warnings;

my $path = "/home/icarus/files";
my $time_var = "-M";

opendir (MYDIR, $path) or die $!;

#from older to newest.  If you want from newest to oldest, switch the
$b for $a then <=> $a for $b
my @files = sort { eval($time_var.' "$path/$b" <=> '.$time_var.'
"$path/$a"') } grep { -f "$path/$_" } readdir(MYDIR);
my @sorted = @files;

closedir(MYDIR);


foreach (@sorted){
   sleep 5; #just to have time to compare
   print "$_\n";
}

This does the same thing as the code I proposed except for the grep
to filter out directories. But it is done in a remarkably awkward way.
Was this something you came up with yourself or was it suggested to you?

What worries me most is that you don't seem to understand why you need
to read the entire directory before you can sort it. If you read a
single directory entry at a time you have only one value. Surely you can
see that it's meaningless to sort a single value?

I hope we can help you to understand this better.

Rob



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to