
kencl
User
Jul 9, 2004, 8:13 PM
Post #2 of 2
(915 views)
|
|
Re: [tacallah] Substitution using variables
[In reply to]
|
Can't Post
|
|
Cute little perl teaser. Try this on for size:
$string = "One, Two, Three"; # note the capture brackets $pattern = qr/,([^,]*)$/; # single quotes otherwise $x's are interpolated $replacement = ' and$1'; # supports as many matches as you can capture @matches = $string =~ /$pattern/; # defined test prevents warnings about uninitialized values in substitution iterator $replacement =~ s/\$(\d+)/$matches[$1 - 1] if defined($matches[$1 - 1])/eg; # now it works :) $string =~ s/$pattern/$replacement/g; >> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!
(This post was edited by kencl on Jul 9, 2004, 8:20 PM)
|