CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Accessing a variable through a variable.

 



arush_pareek
Novice

Oct 22, 2010, 1:03 AM

Post #1 of 4 (841 views)
Accessing a variable through a variable. Can't Post

I know there could be the various ways through which we can implement the below example but I want to know what`s wrong in the below code .

-----------------------------------------------------------
my $var1= "test1";
my $var2= "test2";
my @id=(1,2);

foreach $i (@id)
{
print "value of i is : $i\n";
my $tid = "var" . "$i";
my $tid=${$tid};
print "Test id = $tid \n";
}
-------------------------------------------------------------------------------

The above example does not through any error but is does not prints the value of the $tid. (print "Test id = $tid \n";)

If I remove the my from the first 2 lines it works fine.

---------------------------------------------------------------------------------------
$var1= "test1";
$var2= "test2";
my @id=(1,2);

foreach $i (@id)
{
print "value of i is : $i\n";
my $tid = "var" . "$i";
my $tid=${$tid};
print "Test id = $tid \n";
}
----------------------------------------------------------------------------------

Now the above works fine. I tried many things but could understand what is the problem of scope here.


rovf
Veteran

Oct 22, 2010, 4:34 AM

Post #2 of 4 (835 views)
Re: [arush_pareek] Accessing a variable through a variable. [In reply to] Can't Post

Lexicals are not represented in the symbol table, hence at runtime their names are not known anymore.

Of course I wonder why you are trying to do such evil coding....


BillKSmith
Veteran

Oct 22, 2010, 5:25 AM

Post #3 of 4 (829 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.


Code
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


FishMonger
Veteran / Moderator

Oct 22, 2010, 5:34 AM

Post #4 of 4 (829 views)
Re: [arush_pareek] Accessing a variable through a variable. [In reply to] Can't Post

The reason your code did not give you any errors/warnings is because you told perl that you didn't want to know about your errors. You did that by not including the strict and warnings pragmas, which should be in every Perl script you write.

What you're attempting to do is use symbolic references, which is a really bad idea and only works on global vars, not lexical vars which are declared with the 'my' keyword.

Instead of symbolic references, you should be using a hash.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives