newbie ?: expand variable within a variable

K

Krishna Komoravolu

Hello!

Pardon me for this trivial question.
Is there a way to expand a perl variable with in a variable?
i.e. when I read a line "$line" from an input file,
say the line in text(xml) file is like:
$t:references([@CONTROLLER_TYPE = 0]/@$c) in ....,
when I print this line with print "$line", I want the variables $t and
$c in the above line to be expanded in the output.

In the code segment given below, the last case of the switch i.e.
print "$line" literally prints $t and $c with out expanding them.

while (<IFH>) {
my $line = $_;
SWITCH: for ($line) {
/<TableDef[ ]*name/i &&
do {$line =~ /(")(\w+)/; $t = $2; last;}; # set $t
/<ColumnDef[ ]*name/i &&
do {$line =~ /(")(\w+)/; $c = $2; last;}; # set $c
/<RuleDef/i &&
do {$line =~ /(")(\w+)/; $rule = 1; last;}; # set rule
/<\/RuleDef/i &&
do {$line =~ /(")(\w+)/; $rule = 0; last;}; # unset rule
/[aA-zZ]+/ &&
do {print "$line" if $rule == 1; last;};
}
}

Thanks in advance for any help!

Kris
 
K

Krishna Komoravolu

Please ignore my previous posting.
Found the answer in perlop.html:

$line =~ s/(\$\w+)/$1/eeg;

expands the variables with in $line

Kris
 
G

Gunnar Hjalmarsson

Krishna said:
Is there a way to expand a perl variable with in a variable?
i.e. when I read a line "$line" from an input file, say the line in
text(xml) file is like:
$t:references([@CONTROLLER_TYPE = 0]/@$c) in ....,
when I print this line with print "$line", I want the variables $t
and $c in the above line to be expanded in the output.

perldoc -f eval
 
M

MPBroida

Krishna said:
Is there a way to expand a perl variable with in a variable?
i.e. when I read a line "$line" from an input file,
say the line in text(xml) file is like:
$t:references([@CONTROLLER_TYPE = 0]/@$c) in ....,
when I print this line with print "$line", I want the variables $t and
$c in the above line to be expanded in the output.

Try "perldoc -q expand" and look for one about
expanding variables.

The third example in that chunk worked well for me.

Mike
 
G

Gunnar Hjalmarsson

Gunnar said:
Krishna said:
Is there a way to expand a perl variable with in a variable? i.e.
when I read a line "$line" from an input file, say the line in
text(xml) file is like:
$t:references([@CONTROLLER_TYPE = 0]/@$c) in ....,
when I print this line with print "$line", I want the variables
$t and $c in the above line to be expanded in the output.

perldoc -f eval

Hmm... Kris solved the problem, and the above doesn't seem to help
much, so please disregard it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top