Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: change all occurances of string1 into string2

by choroba (Canon)
on Nov 29, 2012 at 15:29 UTC ( #1006277=note: print w/ replies, xml ) Need Help??


in reply to change all occurances of string1 into string2

Too many curlies. toolic's advice is good, but you can safely remove even more:

s/$thing/$translate{$thing}/g;
Be careful, though, if one translation results in a word that another translation might translate again.
Sometimes, avoiding the inner loop by constructing the regex leads to a faster solution:
my $re = join '|', keys %translate; $re = qr/($re)/; while (<>) { s/$re/$translate{$1}/g; print; }
You might need to add some \b boundaries to the regex so it does not match in the middle of a word.
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: change all occurances of string1 into string2
Select or Download Code
Re^2: change all occurances of string1 into string2
by tobyink (Abbot) on Nov 29, 2012 at 16:10 UTC
    my $re = join '|', map quotemeta, keys %translate;
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (14)
As of 2015-07-06 20:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (83 votes), past polls