Dynamically Creating a 3 Dimensional Array?

:puzzled: This seemingly trivial problem is really confounding me. I have a 3 dimensional array like this …


[font=Courier New]oldmap = [[['a1',0],['a1',1]], [/font]
[font=Courier New]		 [['a0',1],['a1',0]]];[/font]

I simply want to take this array, modify the data, and create a new array. Here’s the code (minus the transform functions):


 
[font=Courier New]newmap = new Array();[/font]
 
[font=Courier New]for (i=0;i<map_height;i++) { [/font]
[font=Courier New]for (j=0;j<map_width;j++) {[/font]
 
[font=Courier New]	old_label = oldmap*[j][0];[/font]
[font=Courier New]	old_type = oldmap*[j][1];[/font]
 
[font=Courier New]	// transform the data[/font]
[font=Courier New]	new_label = transformLabel(old_label);[/font]
[font=Courier New]	new_type = transformType(old_type);[/font]
 
[font=Courier New]	state = [new_label,new_type];[/font]
 
[font=Courier New]	// THIS LINE IS NOT WORKING[/font]
[font=Courier New]	newmap*[j].push(state);[/font]
 
[font=Courier New]} // end j [/font]
[font=Courier New]} // end i[/font]
 
[font=Courier New]trace(newmap);[/font]
 

When I trace “newmap” I always get undefined.

Thanks in advance … been reading the forum for a week now … great info … good to meet you! :slight_smile:

maybe you’ve to make first the arrays then push something…

this is not tested and maybe you’ve to add it a little different but i hope it works…

newmap = new Array();

for (i=0;i<map_height;i++) {
var new_array = new Array();
newmap.push(new_array);
for (j=0;j<map_width;j++) {
var new_array2 = new Array();
newmap*.push(new_array2);
old_label = oldmap*[j][0];
old_type = oldmap*[j][1];

    // transform the data
    new_label = transformLabel(old_label);
    new_type = transformType(old_type);

    state = [new_label,new_type];

    // THIS LINE IS NOT WORKING
    newmap*[j].push(state);

} // end j
} // end i

trace(newmap);

Exactly. In order to use the push () method, you have to tell Flash that newmap’s elements are arrays as well. Pieter’s code is probably correct, but I think that this is enough:

 
[font=Courier New]newmap = new Array();[/font]
 
[font=Courier New]for (i=0;i<map_height;i++) { [/font]
**  newmap* = [] ;**
  [font=Courier New]for (j=0;j<map_width;j++) {[/font]
 
[font=Courier New]	var old_label = oldmap*[j][0];[/font]
[font=Courier New]	var old_type = oldmap*[j][1];[/font]
 
[font=Courier New]	// transform the data[/font]
[font=Courier New]	var new_label = transformLabel(old_label);[/font]
[font=Courier New]	var new_type = transformType(old_type);[/font]
 
[font=Courier New]	var state = [new_label,new_type];[/font]
 
[font=Courier New]	// THIS LINE IS NOT WORKING[/font]
[font=Courier New]	newmap*[j].push(state);[/font]
 
  [font=Courier New]} // end j [/font]
[font=Courier New]} // end i[/font]
 
[font=Courier New]trace(newmap);[/font]