in reply to Array search

First, $_ eq 'abc' is faster than /^abc$/.

If you want 0 or 1:

my $f = !!grep { $_ eq 'abc' } @a; if ($f) { ... }

True or false will probably suffice:

my $f = grep { $_ eq 'abc' } @a; if ($f) { ... }

You can get rid of the flag entirely:

if (grep { $_ eq 'abc' } @a) { ... }

Replies are listed 'Best First'.
Re^2: Array seach
by Aristotle (Chancellor) on Feb 20, 2006 at 00:22 UTC

    More important than that it’s faster is that it expresses what happens more directly. With the pattern match, you have to mentally parse the pattern to figure out what is going on. $_ eq 'abc' is much quicker to read.

    Makeshifts last the longest.

Re^2: Array seach
by jeanluca (Deacon) on Feb 20, 2006 at 08:18 UTC
    nice examples!, but what does !! do ?

    Luca

      It's two negation operators. In scalar context ("!" imposes a scalar context), grep returns the number of matching elements. Negating this twice converts zero to false, and non-zero to true. False is 0 when used as a number. True is 1 when used as a number.

      For example,

      | Ex. 1 | Ex. 2 -------------------+-------+------ grep returns | 4 | 0 1st negation op | false | true 2nd negation op | true | false When used as a num | 1 | 0 When used as a str | '1' | ''