473,387 Members | 1,711 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

variables setting w/array

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
7 1414
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
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
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
> > "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
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
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
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: opt_inf_env | last post by:
Hello, I know three ways to pass variables form one page to another. The first one is to declare and set session variable. In this case if one goes to another page (by clicking on hyperlink or...
7
by: Nicole | last post by:
Hi I'm trying to use a function to set a session variable. I have three files: The first file has: <?php session_start(); // This connects to the existing session ?> <html> <head>
122
by: Einar | last post by:
Hi, I wonder if there is a nice bit twiddling hack to compare a large number of variables? If you first store them in an array, you can do: for (i = 0; i < n; i++) { if (array != value) {...
4
by: PJ | last post by:
A particular page seems to be having issues with correctly setting Session variables. I am setting a couple of session variables on the Page_Unload event. While stepping through code, the...
5
by: Max | last post by:
Hi All! I'm doing an ASP.NET project which uses Persistent Forms Authentication (i.e. once user logged in, they don't have to log in again). However Session variables are erased after Session...
3
by: bennett | last post by:
In the web.config file for my application, in the <sessionState> section I have set timeout="120" (in minutes), but session state variables in my application seem to be expiring in about 5 minutes....
5
by: bill | last post by:
I am a relative newbie to PHP, but not to other languages. I wrote a test script to upload files. In the php section above the page I get the expected content of the $_FILES array. When I have...
8
by: Eddie | last post by:
I am having difficulty in setting variables in a session, and then accessing those variables throughout the web pages that they click on. After having them set a user name and password,...
3
by: iglpdc | last post by:
Hi, I need a template class taking several non-type arguments. Then I want to be able to pass these arguments to the constructor of another non- template class. So far I have this (I did some...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.