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

Variable assignment from variable 1

Status
Not open for further replies.

cdhudson

IS-IT--Management
Oct 17, 2001
1
US
I have two variables $var1 and $var2. The first variable contains the name of a variable that has not been defined: $var1 = '$color'; The second variable contains a value: $var2 = 'green';

I want to assign the value of $var2 to the variable that is stored in $var1. Eg. $color = 'green'; Anyone know how I go about doing this? Thanks in advance,

Dunc.
 
The variable 'var1' should be 'color', instead of 'color$'. Here's how to do it:

$var1 = 'color';
$var2 = 'green';
$$var1 = $var2; # Now $color = 'green'
 
a inappropriately simple answer is,

#!perl
$var1 = 'color';
$$var1 = 'green';
$var2 = $$var1;
print "$$var1 same as $color same as $var2\n";


a better answer is to get a Perl book and read up on references. You might also search this forum for some threads on references. I seem to remember a thread a few months back about passing references to sub routines that ended up having some good examples in it.

Good Luck
I hope this helps. If you are new to Tek-Tips, please use descriptive titles, check the FAQs,
and beware the evil typo.
 
Cool! Can I do the same thing with arrays too? If I had:

$var1 = '@my_array';
$var2 = "('green','blue','red')";

How would I get @my_array to equal ('green','blue','red')?

Dave
 
The easiest way to do this is to leave the @ or $ off of the name. Then you can use this type of syntax:
Code:
$varname = "color";
$varvalue = "green";
${$varname} = $varvalue;
The same type of thing can be done for array naming, but the list of values gets a little trickier. You can either eval the string containing the list of values, or you can create an anoymous array reference to hold the list of values, and then assign that to the array. I prefer the latter method for efficiency. Here's how to do that.
Code:
$arrayname = "colors";
$arrayvalues = ["green","red","blue"]; # note square brackets!
@{$arrayname} = (@$arrayvalues); # note parens and @$ dereference
Notes: You need square brackets around the original list to make it an anonymous array. You "dereference" the anoymous array reference using @$arrayref. You need parens around the deref to make sure it's evaluated as a LIST (array).

I don't think the curly brackets around the variables containing the name of the variable to be created are required, but I always use them to explicitly indicate what I want to do. The value inside the curly brackets will always be evaluated FIRST, then the statement will be re-evaluated. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top