Forum Moderators: coopster & phranque

Message Too Old, No Replies

"spacers" in regexs between variables?

         

andreasfriedrich

6:05 pm on Feb 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Brett_Tabke [webmasterworld.com] wrote at 04:49 on Dez. 31, 2001 in message #1 [webmasterworld.com]
$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?

I just ran into that problem today and used this:

$test =~ s/fuubar/${test}Yes$bar/gi

Andreas

jeremy goodrich

7:40 pm on Feb 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for posting that. Looks like a useful tip.

andreasfriedrich

8:11 pm on Feb 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Today I had the time to go through the history of the Perl Scripting forum. I was just amazed that nobody mentioned that method. In those old days this forum was much more perlish and frequented by a lot of people (you were a regular for some time too, jeremy; do you have jeremey::something modules by now? ;)) who are mods now. It has been a quite interesting journey ;).

gsx

10:10 pm on Feb 5, 2003 (gmt 0)

10+ Year Member



Thats useful. I would have used ($test)Yes but that would reorganise any $0..$9 variables in the substitution.

andreasfriedrich

10:35 pm on Feb 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



gsx [webmasterworld.com] wrote at 10:10 on Feb. 05, 2003 in message #4 [webmasterworld.com]
would reorganise any $0..$9 variables in the substitution.

To use parens for grouping but not storing matches you can use

(?:what¦when)ever
.


$_ = "<title>Nick Carter sucks!</title>";
($verb) = m [perldoc.com]!(?:Aaron¦Nick)\s*Carter\s*([^<]+)</title>!;

$verb would contain "sucks!".

Andreas