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

I have a lot of directories to search here and do not want to put all 100 of them in my 'qw' part. I would like to make it where it searches all my directories except for one direcotory called 'zz'. Here is my current search:
my $dirs = '/directory/subdirectory/webstuff'; find( \&findstuff, map "$dirs/$_", qw(aa bb cc dd ee ff gg hh jj kk ll +));
Can I make my search like this?
my $dirs = '/directory/subdirectory/webstuff'; find( \&findstuff, map "$dirs/$_", qw(!zz));

edited: Mon Nov 18 15:19:52 2002 by jeffa - fixed title typo

Replies are listed 'Best First'.
Re: Searching all directories except one
by Aristotle (Chancellor) on Nov 18, 2002 at 13:16 UTC
    Well no, not like that - Perl can't know what set you want to exclude zz from. You have to read the directory and throw away the entries you don't want.
    my $base_dir = '/directory/subdirectory/webstuff'; my %exclude = map { $_ => undef } qw(. .. zz); opendir my($dh), $base_dir; my @dir = grep { not exists $exclude{$_} and -d "$base_dir/$_" } readd +ir $dh; closedir $dh; find( \&findstuff, map "$base_dir/$_", @dir);
    Update: Fixed code.

    Makeshifts last the longest.

      Do you need to exclude sub-directories from the match or just major trees?

      If you need to exclude the sub-drectories even if the parent is not excluded you may need to be more explicit in your search.

      Just a thought
      UnderMine

        That would be a case for the preprocess predicate to find().

        Makeshifts last the longest.

      I tried as suggested and keep gettin this error:
      Not enough arguments for grep at weba8b.pl line 31, near "} and" Execution of weba8b.pl aborted due to compilation errors.
      Am I missing some sort of "return" statement??
        Sorry, I keep getting tripped by the grep EXPR form. I updated my node; the code that's there now should work.

        Makeshifts last the longest.

Re: Searching all directories except one
by Chief of Chaos (Friar) on Nov 18, 2002 at 14:08 UTC
Re: Searching all directories except one
by Thelonius (Priest) on Nov 18, 2002 at 16:25 UTC
    sub findstuff { if (-d && $_ eq 'zz') { $File::Find::prune = 1; return; } # then other stuff as normal }
    Beware of code like /$dir/ because you might have inadvertent matches, such as with "jazz" instead of "zz".
      But that will dump all directories called zz, even if they're not just at the top level. Furthermore, it will also examine the files at the top level, which the original poster didn't want.

      Makeshifts last the longest.