variables setting w/array

variables setting w/array

Post by dann » Tue, 08 Mar 2005 01:43:07


Hello everyone,

How can I go about setting a number of variables using a table which will
have the variable names to be declared in one table. The data to be assigned
will reside in another. As an example, I know this assignment works:

var nam0 = tab[0]
var nam1 = tab[1]
var nam2 = tab[2]
.
.
.

What I would like is to use two tables

var namtab = ["nam0","nam1","nam2"]
var tab = ["va0","val1","val2"]

Then using a loop, assign the variables so that I may address them by name
"nam0" rather than namtab[0].

var nametab = ["nam0","nam1","nam2"] ** also tried w/o the quotes
for(var i=0;i<3;i++) {
var nametab[i] = crumbs[0];

Thanks,

From a newbie (but learning) .
 
 
 

variables setting w/array

Post by Janwillem » Tue, 08 Mar 2005 02:05:33


Something like this?

var nametab = ["nam0","nam1","nam2"];
var tab = ["va0","val1","val2"];

for (var i = 0; i < nametab.length; i++) {
window[nametab[i]] = tab[i];
}

// Testing
alert(nam0) // Alerts 'va0'


JW

 
 
 

variables setting w/array

Post by Lee » Tue, 08 Mar 2005 02:07:40

danny said:

I can't imagine a situation where you would want to do that.

If you wanted to be able to refer to them as nam[0] rather than
namtab[0], you can do that simply by:

var nam=namtab;

Note that this doesn't make a complete new copy of the array,
it just makes "nam" refer to the same data.
 
 
 

variables setting w/array

Post by dann » Tue, 08 Mar 2005 03:49:34

JW,

Thanks. It worked as expected. Could you just tell me why I needed to use
the statement 'window[nametab[i]] = tab[i];'
instead of just using 'nametab[i] = tab[i];'. Since both tables are in
the same window/document, why must the 'window' method(?) be supplied?

danny



assigned
 
 
 

variables setting w/array

Post by RobB » Tue, 08 Mar 2005 09:20:55


which will
works:
by name
quotes


use
are in
supplied?


Watch the top-posting !

Not a 'window method' - just a way to express an identifier (variable
name) as a string, explained here:

http://www.yqcomputer.com/ #aVa

Instead of cluttering the global namespace with all those variables,
why not populate a global array instead?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
" http://www.yqcomputer.com/ ">
<html>
<head>
<title>untitled</title>
<script type="text/javascript">

var nametab = ["nam0","nam1","nam2"];
var tab = ["val0","val1","val2"];

for (var tabs = [], i = 0, ntl = nametab.length; i < ntl; ++i)
{
tabs[nametab[i]] = tab[i];
}

</script>
</head>
<body>
<form>
<select onchange="alert(tabs[this.value]||'not in')">
<option value="">select</option>
<option value="nam0">nam0</option>
<option value="nam1">nam1</option>
<option value="nam2">nam2</option>
</select>
</form>
</body>
</html>
 
 
 

variables setting w/array

Post by RobB » Tue, 08 Mar 2005 09:30:22


be
them

to
tables

OK, before one of the regulars yells at me: if you're not using that
array for any other purpose - use a plain-vanilla object instead:

for (var tabs = {},....
 
 
 

variables setting w/array

Post by c.anandkum » Tue, 08 Mar 2005 18:07:28

If you really want to use those variables with distinct names of the
form namN, rather than bounding all of them in an array, then use
eval() function. Im pasting a similar function below :

var tabs = new Array ("tZero", "tOne", "tTwo", "tThree");
function variables ()
{
var i;
for (i = 0; i < tabs.length; i++) {
eval ('nam' + i + '=tabs[' + i + ']');
}
alert (nam0);
alert (nam1);
// etc...
}

you pass a valid js statement to eval to cause the statement to be
executed runtime.

Thanks

Anand.
 
 
 

variables setting w/array

Post by Grant Wagn » Wed, 09 Mar 2005 07:02:10


eval() is not required. Assuming you want those variable names to be
global, you could use:

var tabs = new Array ("tZero", "tOne", "tTwo", "tThree");
function variables ()
{
var i;
for (i = 0; i < tabs.length; i++)
{
window['nam' + i] = tabs[i];
}
alert (nam0);
alert (nam1);
// etc...
}

--
Grant Wagner < XXXX@XXXXX.COM >
comp.lang.javascript FAQ - http://www.yqcomputer.com/