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

Assigning Variable from another Variable's Value

Status
Not open for further replies.

Moebius01

Programmer
Oct 27, 2000
309
0
0
US
The title is confusing, but the best way I could think of to describe the challenge at hand. Here's the scenario in a nutshell:
//
var A = 325 (result of a server-side query)
var NUMBER = 'A' (value passed from a previous page)
//

The Goal is to have var NUMBER = 325 (as the value from var A)
Can anyone think of a way to use variable NUMBER's value of "A" to assign it a new value of 325 by reading from variable A?


 
There are two ways to interpret this question.

1) Assign a variable's value from another variable's value
Code:
var A = 325;
var NUMBER = A;
Will assign NUMBER the value of 325

2) Assign a variable's name from another variable's value
Code:
var A = 325;
eval("var " + A + " = 'A'");
Will assign the character 'A' to the variable named 325. Of course this won't work as you must start a variable with an alpha character but you get the general idea.

Hopefully one of these answered your question, Rob
robschultz@yahoo.com
-Focus on the solution to the problem, not the obstacles in the way.-
 
This helped with a slightly different issue, but I left out a third trick in the real problem:
//
var A = 325 (result from query)
var B = 86.3 (also from query)
var ITEM = A (passed from previous page)
//

Here's the real kicker. I need a third variable created from this, using var ITEM as the trigger. The code I'm about to type obviously won't achieve this, but I'm trying to demonstrate the way I need to get the data.

var RESULT = (ITEM)

This would set var RESULT to 'A'. However, I need to figure out a way instead to set it to the value of the variable with the same name, by using the passed variable. So that if ?ITEM=A was passed, RESULT would be set to 325, but if ?ITEM=B was passed, RESULT would be set to 86.3.

Hopefully this clarifies the goal I'm working towards.
 
I think something like:

var A = 325
var B = 86.3

var ITEM = "A"

var RESULT = eval(ITEM)

will probably demonstrate the neccessary 'trick' (if it works - which I'm not all that sure of).

It strike me that you're trying to do what I would call "macro substitution". Is that an accurate characterization?

If so, you should consider what I would call 'pre-substitution' validation issues - what happens to the script when an unanticipated value is assigned to ITEM.

If there is absolutely complete control of the values that may be assigned to ITEM prior to EVALing it, then this issue drops from consideration.

If not, then the script should probably do at least a kind of 'typeof' case check of ITEM as well as an is-not-null check by making sure ITEM contains a valid variable name as a string value.

Hope this helps,
Peter

Peter Vince VA3PKV
va3pkv@rac.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top