By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,625 Members | 1,716 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,625 IT Pros & Developers. It's quick & easy.

variables setting w/array

P: n/a
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) .
Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
danny wrote:
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];


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

Jul 23 '05 #2

P: n/a
Lee
danny said:

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].


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.

Jul 23 '05 #3

P: n/a
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

"danny" <dt*****@planet-inc.net> wrote in message
news:11*************@corp.supernews.com...
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) .

Jul 23 '05 #4

P: n/a
> > "danny" <dt*****@planet-inc.net> wrote in message
news:11*************@corp.supernews.com...
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) .

danny wrote:
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

Watch the top-posting !

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

http://jibbering.com/faq/faq_notes/s...ckets.html#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.w3.org/TR/html4/strict.dtd">
<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>

Jul 23 '05 #5

P: n/a
RobB wrote:
"danny" <dt*****@planet-inc.net> wrote in message news:11*************@corp.supernews.com...
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) .
danny wrote:
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

Watch the top-posting !

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

http://jibbering.com/faq/faq_notes/s...ckets.html#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.w3.org/TR/html4/strict.dtd">
<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>


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 = {},....

Jul 23 '05 #6

P: n/a
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.

Jul 23 '05 #7

P: n/a
<c.**********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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.


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 <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.