Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
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:??;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://141256]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2016-12-05 01:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (71 votes). Check out past polls.