473,396 Members | 1,789 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,396 software developers and data experts.

address array by variable variable

mps
Suppose.....
arr = Array("only","a","test");
ArrayVar = "arr";
alert(ArrayVar[0]);

This the simplyfied version of my problem.. (alerting 'undefined' instead of
'only'...)

Now I use the most ugly workaround:
if(ArrayVar == "arr") { alert(arr[0]); }

But there has to be a much better solution?????

Thanx in advance,
Marco Snoek
Jul 23 '05 #1
9 1286
> Suppose.....


arr = Array("only","a","test");
ArrayVar = "arr";
alert(ArrayVar[0]);

This the simplyfied version of my problem.. (alerting 'undefined' instead of
'only'...)


I think this is what you are looking for:

eval(ArrayVar+"[0]");

But you can also do the following

arr1 = Array("only", "a", "test");
arr2 = Array("only", "another", "test");

curArray = arr1;
alert(curArray[1]); // displays 'a';

curArray = arr2;
alert(curArray[1]); // displays 'another';

Which feels better.

Good luck,
Vincent
Jul 23 '05 #2
Ivo
"mp*@webmind.nl" wrote

arr = Array("only","a","test");
ArrayVar = "arr";
alert(ArrayVar[0]);

This the simplyfied version of my problem.. (alerting 'undefined' instead of 'only'...)

Now I use the most ugly workaround:
if(ArrayVar == "arr") { alert(arr[0]); }

But there has to be a much better solution?????


Not sure what you are trying to do. ArrayVar is a now string variable. If
you remove the quotes from your second line above, so it becomes

ArrayVar = arr;

then ArrayVar will be an array too, and ArrayVar[0] will be "only".
HTH
Ivo
Jul 23 '05 #3
Hello

"mp*@webmind.nl"
<*DONT*[mps]_YOU#DARE(SEND)SPAM@@[webmind]--TOME.[.nl]%%^.orgtext> wrote in
message news:40***********************@dreader5.news.xs4al l.nl...
Suppose.....
arr = Array("only","a","test");
ArrayVar = "arr";
alert(ArrayVar[0]);

This the simplyfied version of my problem.. (alerting 'undefined' instead of 'only'...)

Now I use the most ugly workaround:
if(ArrayVar == "arr") { alert(arr[0]); }

But there has to be a much better solution?????


Yes, you can use eval() as other indicated or you can use something like:

<script>
var x;

array1 = new Array("hello", "world");

x = "array1";

alert(window[x][0]); // <-- hello

</script>
The variables are stored under the window object. You can also do something
like: window["array1"][N] ...

--
Elias
Jul 23 '05 #4
That is my problem: trying to retrieve an array item with a string
variable-name..
You see, the more elaborate version of my problem is

an array with positions:
positions =
("liggend","staand","staand","liggend","staand","s taand","liggend");

an array
liggend[0] = array("value1a","value2a","value3a","value4a","val ue5a");
liggend[1] = array("value1b","value2b","value3b","value4b","val ue5b");
....
staand[0] = array("value1c","value2c","value3c","value4c","val ue5c");
...etc..

Now:
getValue(3) { //For example...
ArrayVar = positions[3]; // in this example ArrayVar = liggend
for(anItem in ArrayVar) {
...... /// walk through the 'liggend-array'..
}
}

I believe the eval function from Vincent does the trick...
Marco

"Ivo" <no@thank.you> schreef in bericht
news:40***********************@news.wanadoo.nl...
"mp*@webmind.nl" wrote

arr = Array("only","a","test");
ArrayVar = "arr";
alert(ArrayVar[0]);

This the simplyfied version of my problem.. (alerting 'undefined'
instead of
'only'...)

Now I use the most ugly workaround:
if(ArrayVar == "arr") { alert(arr[0]); }

But there has to be a much better solution?????


Not sure what you are trying to do. ArrayVar is a now string variable. If
you remove the quotes from your second line above, so it becomes

ArrayVar = arr;

then ArrayVar will be an array too, and ArrayVar[0] will be "only".
HTH
Ivo

Jul 23 '05 #5
Hey,

Great..
It works!!

Thanx all for your time!!

Regards,
Marco
"lallous" <la*****@lgwm.org> schreef in bericht
news:2m************@uni-berlin.de...
Hello

"mp*@webmind.nl"
<*DONT*[mps]_YOU#DARE(SEND)SPAM@@[webmind]--TOME.[.nl]%%^.orgtext> wrote in message news:40***********************@dreader5.news.xs4al l.nl...
Suppose.....
arr = Array("only","a","test");
ArrayVar = "arr";
alert(ArrayVar[0]);

This the simplyfied version of my problem.. (alerting 'undefined'
instead of
'only'...)

Now I use the most ugly workaround:
if(ArrayVar == "arr") { alert(arr[0]); }

But there has to be a much better solution?????

Yes, you can use eval() as other indicated or you can use something like:

<script>
var x;

array1 = new Array("hello", "world");

x = "array1";

alert(window[x][0]); // <-- hello

</script>
The variables are stored under the window object. You can also do

something like: window["array1"][N] ...

--
Elias

Jul 23 '05 #6
Lee
SEND said:

Suppose.....
arr = Array("only","a","test");
ArrayVar = "arr";
alert(ArrayVar[0]);

This the simplyfied version of my problem.. (alerting 'undefined' instead of
'only'...)

Now I use the most ugly workaround:
if(ArrayVar == "arr") { alert(arr[0]); }

But there has to be a much better solution?????


It depends on what you're really trying to do. If you just need
a way to use a variable name for an array, then you can use a
reference, instead of a string holding the name:

var myRef=arr;
alert(myRef[0]);

If you really need to use the string name, then there is probably
a better design that you could use, but one way to do it is:

var arrayGroup = { arr: [ "only", "a", "test" ],
brr: [ "a", "different", "array" ],
crr: [ "one", "more", "example" ]
};
alert(arrayGroup["arr"][0]);
You could also use eval(), but that is inefficient and difficult
to debug.

Jul 23 '05 #7
"Quarco" <*DONT*[mps]_YOU#DARE(SEND)SPAM@@[webmind]--TOME.[.nl]%%^.orgtext> writes:
That is my problem: trying to retrieve an array item with a string
variable-name..
In my experience, you are using a wrong approach to solve the real problem.
I have never seen a case where it was necessary to address a variable
using a string containing its name. Whatever you are trying to do, I'll
almost guarantee I can find a better way to do it :)
an array with positions:
positions =
("liggend","staand","staand","liggend","staand","s taand","liggend");

an array
liggend[0] = array("value1a","value2a","value3a","value4a","val ue5a");
liggend[1] = array("value1b","value2b","value3b","value4b","val ue5b");
...
staand[0] = array("value1c","value2c","value3c","value4c","val ue5c");
..etc..
Instead, do:

var c = new Object(); // container object
c.liggend = new Array();
c.staand = new Array();
and then
c.liggend[0] = ...
c.liggend[1] = ...
c.liggend[2] = ...
...
c.staand[0] = ...
c.staand[1] = ...
getValue(3) { //For example...
ArrayVar = positions[3]; // in this example ArrayVar = liggend
for(anItem in ArrayVar) {
function getValue(i) {
var arrayVar = positions[i];
var arr = c[arrayVar]
for(index in arr) {
var item = arr[index];
// ...
}
...
I believe the eval function from Vincent does the trick...
"eval" is almost never the best solution. It is inefficient and
errorprone, and should be left for the few cases where it can't be
avoided.
"Ivo" <no@thank.you> schreef in bericht
news:40***********************@news.wanadoo.nl...


Please don't top post.

Regards
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #8
"Quarco" <*DONT*[mps]_YOU#DARE(SEND)SPAM@@[webmind]--TOME.[.nl]%%^.orgtext> writes:

On second thought, this solution is even prettier:

---
var liggend = [
["value1a","value2a","value3a","value4a","value 5a"],
["value1b","value2b","value3b","value4b","value 5b"],
...
];
var staand = [
["value1c","value2c","value3c","value4c","value 5c"],
...
];

var positions = [liggend, staand, staand, liggend, staand, staand, liggend];

function getValue(i) {
var arr = positions[i];
for (var index in arr) {
var item = arr[index];
...
}
...
---

Good luck.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #9
Hmmm ..

Think this is the 'ultimate' solution Lasse... :-)

Thanx a lot!

Marco

"Lasse Reichstein Nielsen" <lr*@hotpop.com> schreef in bericht
news:65**********@hotpop.com...
"Quarco" <*DONT*[mps]_YOU#DARE(SEND)SPAM@@[webmind]--TOME.[.nl]%%^.orgtext> writes:
On second thought, this solution is even prettier:

---
var liggend = [
["value1a","value2a","value3a","value4a","value 5a"],
["value1b","value2b","value3b","value4b","value 5b"],
...
];
var staand = [
["value1c","value2c","value3c","value4c","value 5c"],
...
];

var positions = [liggend, staand, staand, liggend, staand, staand, liggend];
function getValue(i) {
var arr = positions[i];
for (var index in arr) {
var item = arr[index];
...
}
...
---

Good luck.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'

Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Hal Vaughan | last post by:
I may have my terms mixed up (I'm not a professional programmer), but if I do this: var array1 = new Array("TestOne", "TestTwo", "TestThree", "TestFour"); var array2 = new Array(); var test1...
27
by: Adam Warner | last post by:
Hi all, In the code snippet below I successfully determine the address of val1:* struct o val1=l_SYM_2B(&a).o; print_aesthetic(&val1); The structure o is heavyweight. I understand...
33
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
0
by: J?n Sveinsson | last post by:
Hello everyone I have been trying to read and write struct to binary files, I'm using to functions to convert the struct to bytes and bytes to struct, I always receive the following error ...
1
by: Jón Sveinsson | last post by:
Hello everyone I have been trying to read and write struct to binary files, I'm using to functions to convert the struct to bytes and bytes to struct, I always receive the following error ...
7
by: ashu | last post by:
look at code #include<stdio.h> int *mult(void); int main(void) { int *ptr,i; ptr=mult; for(i=0;i<6;i++) { printf("%d",*(ptr++));
1
by: Vinod | last post by:
Hi, In VC8 project, I am having a struct which is having a char* variable. Now I am creating a 3 elements array object for the struct. I send the base address of the object using VARIANT to a...
12
by: FI | last post by:
Hello All, I am relatively new to C programming and I am struck with a problem in dynamic memory allocation. I would like to know if it is ok to pass the 'memory address' returned by...
8
by: =?big5?B?r0W84Q==?= | last post by:
Hi All C gurus: Below is a small program to print out the address of array and address of array variable: int main() { char array = "haha"; printf("array:%x\n", array); printf("&array:%x\n",...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.