Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Answer: How would I replace the string 'Red1' with $Red[1]?

by QandAEditors
on Jun 29, 2000 at 17:50 UTC ( #20367=categorized answer: print w/replies, xml ) Need Help??

Q&A > regular expressions > How would I replace the string 'Red1' with $Red[1]?

Use an array (or a hash). What you're trying to do now smells like symbolic refrences, which are just yicky (to use the technical term ) and make your code harder to write and maintain.

You will also need to use the /e modifier (which causes Perl to eval the right-side as an expression).

use strict; my @red; $red[1]='black'; $red[2]='red'; $line =~ s/\$red(\d)/$red[$1]/ge; +

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2016-09-25 06:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:







    Results (464 votes). Check out past polls.