http://www.perlmonks.org?node_id=654748

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hello
in the following code which will remove 'b', 'z', 'y' from the string i can't simplify the:
($1 eq 'b' | $1 eq 'z' | $1 eq 'y') to a more concise one
any suggestions, thanks
#!/usr/bin/perl use warnings; use strict; my $str = "abc xxxssyy zx"; $str =~ s/(.)/($1 eq 'b' | $1 eq 'z' | $1 eq 'y')? "" : $1/egs; print $str;

Replies are listed 'Best First'.
Re: remove multiple characters
by bart (Canon) on Dec 04, 2007 at 10:52 UTC
    First of all, don't use "|" as a boolean operator. It is not, it is a bitwise operator. Use "||" instead, for booleans.

    And second... why aren't you trying to match just 'b', 'z' or 'y' characters, instead of absolutely anything? That's what regular expressions are for!

    $str =~ s/([bzy])//g;

    p.s. The capturing parens aren't necessary, but perhaps you may have a use for them yet, so I let them in... But, for this code snippet, you can leave them out. The replacement will probably be faster for it.

    $str =~ s/[bzy]//g;

      Hi bart

      I think dont want that grouping also, you can directly substitute as

      $str =~ s/[bzy]//g;

      Punitha

        It's not just grouping, it's capturing too.

        And I had just finished updating my node, justifying what I did there.

Re: remove multiple characters
by fenLisesi (Priest) on Dec 04, 2007 at 10:58 UTC
    $str =~ tr/bzy//d;