Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

remove multiple characters

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

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;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-03-19 02:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found