I think John has answered your immediate question.

If you want to get the files back in a particular order you should include
a sort between and grep readdir. An example might be

my @files = sort { $a cmp $b }
                   grep { ! /^\./ && -f "$dir/$_" } readdir($dh);
for (0..$#files) {
    print "$_) $files[$_]\n";
}


Alternatively, if you want the files in size order you could use something
like this

my @files = map { $_->[0] }
                   sort { $a->[1] <=> $b->[1] }    # numeric sort
                   map { [$_,  -s "$dir/$_"] }
                   grep { ! /^\./ && -f "$dir/$_" } readdir($dh);

for (0..$#files) {
    print "$_) $files[$_]\n";
}

If you want to get them back in an order based on the file modification
time, you would need to stat the file,

my @files = map { $_->[0] }
                   sort { $a cmp $b }
                   map {[$_, (stat("$dir/$_"))[9] ] }
                   grep { ! /^\./ && -f "$dir/$_" } readdir($dh);


Hope that helps,
Dermot.



On 9 January 2015 at 01:52, Harry Putnam <rea...@newsguy.com> wrote:

> Opening a directory and readdir with a grep in there to find specific
> filenames, how does that process collect the files?
>
> I mean will the generated @ar of files be oldest first or someother
> reliable order?
>
> Using an example paraphrased from perldoc -f readdir:
> (I changed the regex)
>
>   opendir(my $dh, $some_dir) || die "can't opendir $some_dir: $!";
>   my @a_ar = grep { /^a/ && -f "$some_dir/$_" } readdir($dh);
>   closedir $dh;
>
> Will all the files beginning with `a' that make it to @a_ar, always be
> in order of modtime, oldest first? Or some other reliable order?
>
> Also, is there a similar reliable way files are processed when using
> File::Find?
>
> The perldoc page mentions that its depth first, but then what. What
> factors are considered in choosing a file to process?
>
> Of course, I mean beyond whatever specifications the script imposes.
>
>
>
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>

Reply via email to