Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Re: variables in substition/evalby kcott (Archbishop) |
on Aug 18, 2017 at 06:33 UTC ( [id://1197594]=note: print w/replies, xml ) | Need Help?? |
G'day xaphod, [Assumption: As already stated, you need to tell us what you do expect, not just that you didn't get what you expected. I'm assuming that, in the three blocks of output, you want "huey & donald", "dewey & donald" and "louis & donald", respectively.] AM has already pointed out the issue. In Perl v5.14, non-destructive substitution was introduced, which could get around this problem (see "perl5140delta: Non-destructive substitution" for details and additional information). It looks like you've probably over-engineered your solution and, as result, generated the unexpected output. Your nested loops could have been written like the following: I've commented out all the over-engineered code so you can see how much simpler it could have been.
That produces the output as per my "Assumption". If your version of Perl is older than 5.14, which is now over six years old[perlhist], consider upgrading but, for now, you'll need to code this the old way by assigning the original text to a new variable first, then performing the substitution on that new variable, leaving the original unchanged. By the way, in your original regex, you used the 'g' modifier. This is completely unnecessary: it's for performing multiple matches in a single regex; not for single matches that happen to be performed on multiple occasions. These three one-liners should show the difference:
That's just a guess as to why you thought you needed a 'g' modifier. See "perlre: Modifiers" for details. — Ken
In Section
Seekers of Perl Wisdom
|
|