...

View Full Version : Using variable values to reference the name of another variable/array



aatwo
11-07-2006, 08:43 PM
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;


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...



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 :)

VortexCortex
11-07-2006, 08:56 PM
In math we call this type of data structure a matrix.


/*
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++){
alert(myArrays[i]);
}


For example: you can refrence the 2nd element of the 4th array in myArrays by using this:
myArrays[4][2]

aatwo
11-07-2006, 09:15 PM
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

VortexCortex
11-07-2006, 09:15 PM
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...


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+'"');
}
}

alert(myArray0);
/*
Or this would have worked with the previous function:
myArrays= createMatrix(4,5,'something');
alert(myArray[0]);
*/



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.

aatwo
11-07-2006, 09:26 PM
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.

VortexCortex
11-07-2006, 09:26 PM
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...



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.


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


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+'"');
}

aatwo
11-07-2006, 09:54 PM
*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 :D

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?

aatwo
11-07-2006, 10:05 PM
Oh never mind it works perfectly. I just made a silly mistake. thanks again.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum