Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
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 cooling their heels in the Monastery: (4)
As of 2015-07-30 01:13 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 (269 votes), past polls