Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: multiple (different) substitutions on same $foo

by matsmats (Monk)
on Aug 22, 2003 at 14:48 UTC ( [id://285796]=note: print w/replies, xml ) Need Help??


in reply to multiple (different) substitutions on same $foo

Here's an alternative way to do it, doing the whole search and replace as a oneliner:

my $foo = "The frog kissed the woman and became a man."; $foo =~ s/\b(frog|woman|man)\b/{frog=>toad,woman=>girl,man=>boy}->{$1} +/eg;

I've added a \b for you, see comment further up

This assumes you meant to have a /g-switch after every substitution. I couldn't find a way to match only the first "man" and "woman", but I'm sure it will keep me awake tonight.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-19 06:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found