Click to See Complete Forum and Search --> : Array Switching


Falconix
04-29-2003, 08:15 PM
Say I have array called "index". It contains these values:

index[0] = 3;
index[1] = 6;
index[2] = 2;
...

I want to access an array called "files," in this way:

var info = files[index[0]][index[1]][index[2]][index[...]];

The catch is, "index" can contain any amount of index/variable pairs. I can find out that amount easily; that isn't the problem. Does anyone know how I can access "files" in a way where the amount of dimensions there are is determined by the size of "index"? I've been experimenting, but can't really come up with anything.

:)

pyro
04-29-2003, 08:21 PM
What is it exactly that you are trying to do? If you explain where you are trying to go, it will make it easier to point you in the right direction. Do you need info to contain all the values of index[x]?

Falconix
04-29-2003, 08:32 PM
Well... I have an array called files, with lots of pre-entered values in it. I have a function called add(path) which takes an arguement in the form of "int/int/int/..." IE "1/4/2/7". So far, all it does is this:

index = path.split("/");

The point of add(path) is to add the value contained at files[int][int][int][...] to an already present string. For example, if I did

add("3/5");

I would expect the return value to be whatever lies at files[3][5]. This would be easy, if there were a predetermined length of "path".

See the problem?

Falconix
04-29-2003, 09:51 PM
Yes... the function should be able to return an array or a string. :) What happens to the return value of add after the function is called is irrelevent to the problem; however, it will be tested for type, and dealt with accordingly.

Falconix
04-30-2003, 07:08 AM
Hmmm... It doesn't seem to work. Here is excactly what I did... the function is just what you wrote:

function onClickDir(path)
{
return eval("files."+(path.split("/").join(".")));
}

And then I checked the return value by making a form, and using this:

document.form1.text1.value = onClickDir("1/3");

And nothing came in the form's textbox, which is what text1 refers to.

Is there anything wrong with the way I'm using it?