Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable interpolation

Status
Not open for further replies.

RPrinceton

Programmer
Jan 8, 2003
86
US
Hi,
In the book "Learning Perl" it appears that the language supports variable interpolation. What I want to do is dynamically change the name of a variable in a loop so that when the Perl interpreter sees it, it will appear to have a new name with each iteration. Please review the following code that is not working.
my $i; # field name index
my $base = ""; #base field name
my $base1 = "abc";
my $base2 - "def"
while ($i <= 1)
{
$i = $i + 1;
$base = '$base'.$i;
print LOG &quot;Field: $base\n&quot;;
^
|__ I would think 1st iteration would
be named $base1 and in the log &quot;abc&quot;
2nd iteration would be named $base2
and in the log &quot;def&quot;
}
Here is what I end up in the log:
Field: $base1
Field: $base2
I am sure there is a little &quot;tweak&quot; to accomplish this.
Please advise.
Thanks in advance.
Regards,
Randall Princeton


 
Maybe this is the tweak you need :

$var1 = &quot;Var 1&quot; ;
$var2 = &quot;Var 2&quot; ;

for(1..2){
$varname = &quot;var&quot;.$_ ;
print ${ &quot;varname&quot; } ;
}


Hope this helps.

 
Hi Siberian,
Thanks for your help!
I cut and pasted your example directly into my program and adjusted the for(1..2) to for(2) and the print statement to print LOG ${&quot;varname&quot;}; so I can drive the results to my log file. In my log file I end up with var1 and var2 instead of &quot;var 1&quot; and &quot;var 2&quot; respectively. This tells me that the variable value is not being substituted. Is there something additional that must be coded? Please advise.
Thanks in advance.
Regards,
Randall Princeton
 
Oops use :

print ${ &quot;$varname&quot; }

instead of

print ${ &quot;varname&quot; } ;


Sorry about that! You need to derefence the full variable contents into another variable name. Doh!

---full script--
$var1 = &quot;I am number 1!&quot; ;
$var2 = &quot;So what, I am number 2!&quot; ;

for(1..2){
$varname = &quot;var&quot;.$_ ;
$var1 = &quot;I am number 1!&quot; ;
$var2 = &quot;So what, I am number 2!&quot; ;

for(1..2){
$varname = &quot;var&quot;.$_ ;
print ${ &quot;$varname&quot; },&quot;\n&quot; ;
}


 
Hi Siberian,
Thanks again.
I have made the modification i.e.,
print ${ &quot;$varname&quot; }
instead of
print ${ &quot;varname&quot;}

to my script and now in my log file the variables contain empty strings. Any other ideas?
Please advise.
Thanks in advance.
Regards,
Randall Princeton
 
Paste your full script, I think you are probably using the wrong variable names or some such thing.

Thanks
 
Good morning Siberian,
You are absolutly right! After reviewing my script I had
variables var1 and var2 defined with 'my' in another part of the script. Once I removed the &quot;my instantiations the script worked properly. You are a life saver! I will now be able to dynamically create variable names and process 80 database fields having column names like col1, col2 col3 etc. in a loop structure.
Thanks again for your help.
Regards,
Randall Princeton
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top