in reply to Another question about expanding variables in text strings

First, your regex wasn't matching. For example, \w does not match '[' as would be necessary for your expression to succeed. And \w does match '_' which complicates things since you have '_' as a char right after the variable you want to interpolate.

Second, if you are using use strict; (recommended) and variables declared with 'my', you need to use the approach for lexicals given on the page you mention:

my @Val = qw(aaa bbb ccc); my $str = 'TESTDATA_BETA$Val[1]_DEFL$Val[2].$Val[0]'; if ($str =~ s/(\$[^]]+\])/$1/gee) { print "$str\n"; }
This assumes all the interpolated values are array values. If that is not the case, a different approach will be needed in the first half of the regex. Perhaps something like: s/(\$[a-zA-Z0-9[\]]+)/$1/gee which also works with the example data you gave.

Replies are listed 'Best First'.
(tye)Re: Another question about expanding variables in text strings
by tye (Sage) on Mar 15, 2001 at 04:51 UTC

    Starting from something I rolling in (tye)Re: Trying to understand some body's old code.: s/(\$[\w\[\]{}']+)/'"'.$1.'"'/gee; I thought that wouldn't work for this case and went about making elaborate changes... But then I realized that the original should work just fine (and even tested it). (:

            - tye (but my friends call me "Tye")
Re: Re: Another question about expanding variables in text strings
by Anonymous Monk on Mar 24, 2001 at 03:07 UTC
    Thanks for the help. I'm not sure I totally understand the pattern matching string, but I'll do some digging in the Programming Perl book. The most important thing is it works :)