in reply to grep usage confusion

my @index2 = grep($_ ne "fourthy", @coins);

I guess you meant

my @index2 = grep {$_ ne "fourthy"} @coins);
or
my @index2 = grep('$_ ne "fourthy"', @coins);
because in the way you have written it, it would not work (grep needs to eval the first argument (block or string) repeatedly for each item in the argument list).

-- 
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re^2: grep usage confusion (eval)
by tye (Sage) on Oct 14, 2008 at 18:07 UTC

    No, grep doesn't eval strings of Perl code. You can give it a block or you can give it an expression (not a string). Your first example does the same thing as the original code (which does work) while your second example returns all of @coins since the string '$_ ne "fourthy"' is a true value.

    - tye        

      grep doesn't eval strings of Perl code.

      Oops, you are right! My mistake!

      as the original code (which does work)
      Thanks for pointing that out. Interesting. I was not aware of that usage of grep (always used it with a block).

      -- 
      Ronald Fischer <ynnor@mm.st>