
BillKSmith
Veteran
Oct 22, 2010, 5:25 AM
Post #3 of 4
(741 views)
|
|
Re: [arush_pareek] Accessing a variable through a variable.
[In reply to]
|
Can't Post
|
|
You are attempting to use "Symbolic References". Refer to the section with that name in perldoc perlref for the explanation of why your first case does not work. The use of symbolic references is generally considered poor (or worse) programming style. It is almost always possible to use a hash instead.
use strict; use warnings; my %var_hash = ( var1 => 'test1', var2 => 'test2', ); my @id = (1,2); foreach my $i (@id){ print "value of i is: $i\n"; my $tid = $var_hash{"var$i"}; print "Test id = $tid\n"; } Good Luck, Bill
|