in reply to Re: 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.

Array is passed in that has lines, say,
aaa,
bbb,
ccc.

Create empty array for results, is 'aaa' in it? No? add to results, next, is 'bbb' in results array? (well I just added 'aaa', and that's it, so results should be no, BUT results are 'yes').

That's the problem.

Why is 'bbb' in an array that only contains 'aaa'?

  • Comment on Re^2: Braino - why is this not working?

Replies are listed 'Best First'.
Re^3: Braino - why is this not working?
by Anonymous Monk on Oct 19, 2010 at 01:22 UTC
    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.
      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!