Forum Moderators: coopster & phranque

Message Too Old, No Replies

"spacers" in regexs between variables?

         

Brett_Tabke

4:49 am on Dec 31, 2001 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



$test ="foo";
$bar ="bar";
$test =~ s/fuubar/$testYes$bar/gi

See how $test runs on into "Yes" in the replacement? That's going to be interpreted as a variable with the name "$testYes" instead of $test + "Yes".

What do you use for a spacer to get that interpreted right?

Key_Master

5:57 am on Dec 31, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good question. I never ran into this problem but would be interested in a better answer. The only way I know how to do it is:

$test ="foo";
$bar ="bar";
$_ = "".$test."yes".$bar."";
$test =~ s/fuubar/$_/gi;

Or change one of the variables name.

sugarkane

12:01 pm on Dec 31, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Simplest way is with the 'e' modifier, plus periods:

$test =~ s/fuubar/$test.Yes.$bar/gie;

The e modifier indicates that the second half of the regex should be evaluated as a perl expression, so in this case the periods are interpreted as 'concatenation' characters - effectively they become spacers.

netcommr

11:21 pm on Jan 3, 2002 (gmt 0)

10+ Year Member




Brett, just escape the 'Y' (works in most cases)

s/fuubar/$test\Yes$bar/gi;

Brett_Tabke

12:09 am on Jan 4, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Well that's it. Thanks Danny.

That works ok netcommer for about 15 out of 26 characters. (eg \n \t...)

netcommr

12:23 am on Jan 4, 2002 (gmt 0)

10+ Year Member




like I said...most cases... ;)