Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Using variable values to reference the name of another variable/array

1. ## Using variable values to reference the name of another variable/array

Hi. I have am writting a small program in Javascript and I came across a small problem. I thought up a solution however I am having a few problems implementing it.

Basically I have 5 arrays, say;
Code:
```myArray0
myArray1
myArray2
myArray3
myArray4```
I labelled the names of my arrays so that they all end with incrimenting numerical values. Now what I'm hoping to figure out is how to loop through all of the arrays using a loops to the effect of something like this...

Code:
```for(i=0; i<=4; i=i+1){
myArray i [0] = "something";
}```
So it starts by setting myArray0[0] to "something" and then finishes by setting myArray4[0] to "something".

Thanks for any help

• ## Array of Arrays.

In math we call this type of data structure a matrix.
Code:
```/*
Returns an array with x elements each of which holds an array of y elements.
optionally fills the matrix with a value (val)
*/
function createMatrix(x,y,val){
var ar = new Array(x);
for (var i=0;i<x;i++){
ar[i] = new Array(y);
if (typeof val != 'undefined') for(var j=0;j<y;j++){
ar[i][j]=val;
}
}
return ar;
}
var myArrays = createMatrix(5,4);
var count = 0;
for (var i = 0; i < 5; i++){
for(var j = 0; j < 4; j++){
myArrays[i][j] = count++;
}
}
for (var i = 0; i < myArrays.length; i++){
}```
For example: you can refrence the 2nd element of the 4th array in myArrays by using this:
Code:
`myArrays[4][2]`

• So basically what you are doing there is creating a different array inside each part of another array?

Is this an accurate visualisation of whats happening?

y1 contains a1 a2 a3 a4 etc
y2 contains b1 b2 b3 b4 etc
y3 contains c1 c2 c3 c4 etc
y4 contains d1 d2 d3 d4 etc
etc

If so then I can see how that would be very useful. But I do infact have several problems that would be solved using the solution to my original problem.

I am also trying to loop through several variables eg:

variable1
variable2
variable3
etc

• On second thought you could also use the eval funciton, but I would stay away from eval, it's slower than the previous suggestion, and requires string concatenation which is slow...
Code:
```myArray0 = new Array();
myArray1 = new Array();
myArray2 = new Array();
myArray3 = new Array();

var str = 'something';

for (var i = 0; i < 4; i++){
for(var j = 0; j < 5; j++){
eval('myArray'+i+'['+j+']="'+str+'"');
}
}

/*
Or this would have worked with the previous function:
myArrays= createMatrix(4,5,'something');
*/```
This dynamicly creates the lines of code to put the number at the end of "myArray"... Only use this method if you are stuck with the myArray0 to myArray1 variables.

• I just implemented the eval() function and it does perfectly what I am trying to accomplish. Also thank you very much for your quick replies and for teaching me the basic conepts of matricies. I think I will go about converting the relevant parts of my program using the matricie methodology since I can see them being much more efficient to how I was trying to store my data.

Thanks again.

• Originally Posted by aatwo
So basically what you are doing there is creating a different array inside each part of another array?
Yes, an array whos elements are arrays...

Originally Posted by aatwo
If so then I can see how that would be very useful. But I do infact have several problems that would be solved using the solution to my original problem.

I am also trying to loop through several variables eg:

variable1
variable2
variable3
etc
Use an array instead of variable1 to variable3.
Code:
```variable = new Array(3);
for (var n = 0; n < variable.length; n++) variable[n] = 'something';```
Try to design your code better instead of falling back on the eval function.
99% of the time the eval function can be avoided by rewriting the code to work the way it needs to.

In other words, if you are going to have a bunch of variables that are sequentially numbered, you should just use an array because it is what you really wanted to do--have a single variable name that holds a list of values numbered sequentially.

Reluctantly, Here's how to do the above with eval function
Code:
```var variable0='tacos';
var variable1='are';
var variable2='tasty';
var variable3='food';

var something = 'yep';

for (var i = 0; i < 4;i++){
eval('variable'+i+'="'+something+'"');
}```

• *Edit* made a change to the code nearest the bottom

I am literally one line away from completing my code using the eval function (I hope) but as you strongly suggest I will look into making it more efficient afterwards. But being so close I understandably would like to just take the extra step to get it finally working

basically I am trying to make this line of code more dynamic by changing myArray0 to what would be eval('myArray' + number)

if(myarray0[i]==0){
code
}

however java script seems to have a problem with me writing...

if(eval('myArray' + number + '[' + i + ']')==o){
code
}

I have to implement the eval functions in several ways to this line of code but all seems to cause it to error. Do you know why?

• Oh never mind it works perfectly. I just made a silly mistake. thanks again.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•