Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Re: $1 in variable regex replacement string

by jsprat (Curate)
on Feb 13, 2003 at 01:57 UTC ( [id://234882]=note: print w/replies, xml ) Need Help??


in reply to Re: $1 in variable regex replacement string
in thread $1 in variable regex replacement string

This thread has turned into the "Perl Quiz of the Week" #1! ;-)
Write a subroutine, 'subst', which gets a string argument, $s. It should search $s and replace any occurrences of "$1" with the current value of $1, any occurrences of "$2" with the current value of $2, and so on.

For example, if $1, $2, and $3 happen to be "dogs", "fish" and "carrots", then

subst('$2, $1 and $3')
should return
"fish, dogs, and carrots"
dominus' post-mordem analysis of this problem can be found here. He offers several solutions and some discussion of why (and why not) to use them.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-19 04:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found