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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A variable of which the name is stored in another variable 1

Status
Not open for further replies.

serpento

Programmer
Jun 16, 2002
92
GB
If I have the name of a variable stored in another variable, can I read the contents of the variable? EG:
Code:
  $nameOfVariable = "foo";
  $foo = "contents of variable that I want to read";
  # Now, supposing that $foo could be anything,
  # eg. $sweetcorn, is it possible to read the contents
  # of the variable using $nameOfVariable?
Any help would be much appreciated!

-Ed ;-)

________________________________
Destiny is not a matter of chance; it is a matter of choice.
 
Code:
#!usr/bin/perl
print "start\n";
$nameOfVariable="foo";
$foo="contents to be read";
$tmp="\$$nameOfVariable";
print eval("$tmp")."\n";
print "finished\n";

There's probably an easier way
HTH
--Paul
 
Thanks Paul, thats solved my problem brilliantly! Have a star.

-Ed ;-)

________________________________
Destiny is not a matter of chance; it is a matter of choice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top