in reply to Using a Regexp to match a string exactly

In Perl, there's no restriction for use of grep to just regexes! So: just use eq (or its inverse: ne) already.
my $C = shift ; my @others = grep $_ ne $C, @_;

p.s. Unless there are more parameters following the grep call not intended for (this) grep, you may just as well drop the parens.

Replies are listed 'Best First'.
Re^2: Using a Regexp to match a string exactly
by brainbuz (Novice) on Jan 28, 2010 at 03:17 UTC

    Thanks, I was unaware that grep supported eq/ne!

      Not just eq/ne... but any expression that returns a true or false value — which implies: any expression in scalar context. (undef, "" and 0 are false, anything else is true.)

      The generic variable $_ holds the value of the current item from the list. Actually, it's even stronger than that: $_ is an alias to the current item, which means: same value (by reference; you could say: same variable); different name. If you modify $_ in that expression, the original value will have changed. Example:

      @original= (0 .. 5); @true = grep $_*=2, @original; local $" = ", "; # for nicely formatted output print "original: @original\n"; print "output: @true\n";
      Result:
      original: 0, 2, 4, 6, 8, 10 output: 2, 4, 6, 8, 10

      Each item in the original array has been doubled. Of those, only the nonzero (true) values have been come through the grep filter.