Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone how to limit Find(), meaning to stop traversing
at a certain number? For example: if i wanted to
pick up the 1st ten files a dir, how can i make Find()
this?thanks

Replies are listed 'Best First'.
Re: Limit Find();
by dmmiller2k (Chaplain) on Jan 31, 2002 at 20:31 UTC

    A strong case for RTFM, if I ever saw one. Try looking at File::Find. What you want is to customize the wanted sub.

    dmm

    If you GIVE a man a fish you feed him for a day
    But,
    TEACH him to fish and you feed him for a lifetime
Re: Limit Find();
by tachyon (Chancellor) on Jan 31, 2002 at 20:50 UTC
    Find ( 3, \&english_bobby ); # Find( number_to_find, sub_ref_to_do ) sub Find { my ( $num, $sub ) = @_; &$sub for 1..$num; print "What have we here?\n"; } sub english_bobby { print "Hello " }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Limit Find();
by gav^ (Curate) on Jan 31, 2002 at 20:41 UTC
    This works... but is horribly inefficient because it doesn't stop searching through the files just printing them.
    use File::Find; find(\&wanted, $ARGV[0]); my %dir = (); sub wanted { if ($dir{$File::Find::dir}++ < 10) { print $File::Find::name, "\n"; } }
    Update: Seems a bit harsh to downvote me for giving some code that answers the question...

    gav^

      Wouldn't this work?
      use File::Find; eval {find(\&wanted, $ARGV[0])}; print "@found"; my @found = (); sub wanted { push @found, $_; die if @found == 10; }

          p