in reply to Re: How does find() build its tree.
in thread How does find() build its tree.

Is there any way to sort what find returns by the time
stamp?
  • Comment on Re: Re: How does find() build its tree.

Replies are listed 'Best First'.
Re: Re: Re: How does find() build its tree.
by kjherron (Pilgrim) on Jan 28, 2002 at 22:41 UTC
    File::Find just retrieves all of the filenames and calls your "wanted" function once on each name. Anything more sophisticated has to be implemented by you in terms of this "wanted" function.

    For example, the following script will perform a find on /tmp, then print all of the file names in order from most recent to oldest:

    #!/usr/bin/perl -w use strict; use File::Find; my @files; sub wanted { push @files, [ $File::Find::name, -M $_ ]; } find(\&wanted, "/tmp"); foreach (sort {$a->[1] <=> $b->[1]} @files) { print $_->[0], "\n"; }
    As you can see, within the wanted function, we collect each filename and the time it was last modified. Once find() finishes, we then sort the files into the desired order and print them out.
Re: Re: Re: How does find() build its tree.
by chromatic (Archbishop) on Jan 28, 2002 at 23:56 UTC
    See the preprocess argument -- if you pass a code reference, it'll apply it to the list returned by readdir before calling your wanted coderef. Something like this, perhaps:
    find({ wanted => \&wanted, preprocess => sub { sort { -M $a <=> -M $b } @_; }, }, '.');
      Just to let you juys know, I think find returns the
      files in the order of 'ls -f'. Unless someone knows
      something else. Thanks for all your help.