Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: tr///

by japhy (Canon)
on Jan 24, 2002 at 21:42 UTC ( #141256=note: print w/ replies, xml ) Need Help??


in reply to Replacing multiple words in a regex

tr/// is strictly for character-to-character transliteration. You want substitution.

%change = qw( green yellow blue red ); my $rx = join '|', # alternation map { quotemeta } # make it a safe string sort { length($b) <=> length($a) } # so that 'broken' matches before 'broke' keys %change; $str =~ s/($rx)/$change{$1}/g;
This is a common idiom.

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;


Comment on Re: tr///
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2014-10-30 16:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls