Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

remove multiple characters

by Anonymous Monk
on Dec 04, 2007 at 10:44 UTC ( #654748=perlquestion: print w/replies, xml ) Need Help??

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

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;


        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;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://654748]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2020-10-26 16:27 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (252 votes). Check out past polls.