473,659 Members | 2,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1300
> 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. xs4all.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","sta and","staand"," liggend","staan d","staand","li ggend");

an array
liggend[0] = array("value1a" ,"value2a","val ue3a","value4a" ,"value5a");
liggend[1] = array("value1b" ,"value2b","val ue3b","value4b" ,"value5b");
....
staand[0] = array("value1c" ,"value2c","val ue3c","value4c" ,"value5c");
...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.n l...
"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.o rg> 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. xs4all.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(ArrayV ar[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(arrayGrou p["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","sta and","staand"," liggend","staan d","staand","li ggend");

an array
liggend[0] = array("value1a" ,"value2a","val ue3a","value4a" ,"value5a");
liggend[1] = array("value1b" ,"value2b","val ue3b","value4b" ,"value5b");
...
staand[0] = array("value1c" ,"value2c","val ue3c","value4c" ,"value5c");
..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.n l...


Please don't top post.

Regards
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.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","valu e2a","value3a", "value4a","valu e5a"],
["value1b","valu e2b","value3b", "value4b","valu e5b"],
...
];
var staand = [
["value1c","valu e2c","value3c", "value4c","valu e5c"],
...
];

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/rasterTriangleD OM.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","valu e2a","value3a", "value4a","valu e5a"],
["value1b","valu e2b","value3b", "value4b","valu e5b"],
...
];
var staand = [
["value1c","valu e2c","value3c", "value4c","valu e5c"],
...
];

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/rasterTriangleD OM.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
2914
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 = "ScalarOne"; var test2 = test1; array2 = array1;
27
2231
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 (hopefully correctly) that (barring compiler optimisations) C will shallow copy the structure into val1.
33
3155
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
2090
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 C:\Documents and Settings\jon.JONHS-LAP\My Documents\Visual Studio Projects\StructTest\Class1.cs(100): Cannot take the address or size of a variable of a managed type ('StructTest.test')
1
8000
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 C:\Documents and Settings\jon.JONHS-LAP\My Documents\Visual Studio
7
5819
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
1533
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 function where I am casting to struct again using reinterpret_cast. Then If I am trying to traverse the array object from the index 0 to 2, it is working fine, If I am going for the index 3rd element, that char* variable
12
1957
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 malloc()(stored not in a pointer variable but as an element of an array of type int*) directly to free(). My program allocating and freeing memory this way is corrupting the heap. Can anybody guide me on this. Thanks in advance
8
15017
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", &array); }
0
8427
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8330
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8850
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.