Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2015-08-29 15:43 GMT
Find Nodes?
    Voting Booth?

    The oldest computer book still on my shelves (or on my digital media) is ...

    Results (343 votes), past polls