in reply to Re^2: Braino - why is this not working?
in thread Braino - why is this not working?

It's the $_ from the 'foreach' from the array that is passed.

The closes such construct is the map, not the foreach

my @a = 1 .. 3; my @res = 0; for(@a){ my $ans = grep { warn ">>$_<< "; /^$_$/ } @res; warn "ans >>>$ans<<< " } __END__ >>0<< at - line 4. ans >>>1<<< at - line 8. >>0<< at - line 4. ans >>>1<<< at - line 8. >>0<< at - line 4. ans >>>1<<< at - line 8.

Replies are listed 'Best First'.
not exactly, but yeah, anyway... ;-)
by perl-diddler (Chaplain) on Oct 19, 2010 at 03:35 UTC
    Details slightly off, but the essence was it.

    Detail: the foreach works sets "$_" to each value in the list, in turn

    print "_=$_\n" for (1..3);
    but ...

    Your post hit the reason for the problem. "Grep" sets $_ to each member in turn, so searching for '$_' will always return 'true' if you search on /$_/;

    Absolutely simple, got caught with my 1 'register' var being double used. Sigh. Thinking about the simplicity of using $_ in 'foreach', I forgot about grep...sigh.

    MUCH thanks -- very it was driving me crazy!