in reply to Better way to search?

Use a hash.
my %exts; @exts{@exts} = (); sub wanted { if (/\.(\w+)/) { print "$File::Find::dir/$hold\n" if exists $exts{lc($1)}; } }
grep would also do.

This is untested, but the implementation can't be too far different.

Replies are listed 'Best First'.
Re: Re: Better way to search?
by Kanji (Parson) on Jan 05, 2001 at 06:58 UTC

    If you really wanted to do away with "bloat", you could condense chromatic's example further ...

    sub wanted { print "$File::Find::dir/$hold\n" if /\.(\w+)$/ and exists $exts{ lc $1 }; }

    ... which reads a little more natural (at least to me :-)).

    You might also consider adding a small snippet of code so directories with multiple matches are only printed once instead of once per match.

    my %already_seen; sub wanted { print "$File::Find::dir/$hold\n" if /\.(\w+)$/ and exists $exts{ lc $1 } and not $already_seen{ $File::Find::dir }++; }

        --k.


Re: Re: Better way to search?
by r.joseph (Hermit) on Jan 05, 2001 at 06:15 UTC
      It's a hash slice.

      It fills the hash %exts with the contents of your @exts array. The keys are set to the values in @exts, and the values in the hash are set to undef. That's why you need to test for exists rather than "truth"; because the values aren't "true", but the keys do exist.

        Ah, I see. So if I have this:
        @exts = {'jpg','jpeg','gif'); %exts; @exts{@exts} = ();
        then we get an %exts array that is the same as this:
        %exts = ('jpg'=>'','jpeg'=>'','gif'=>'');
        Right? Why do you so @exts{@exts} and not %exts{@exts}?