automating nested arrays with split

going from actionscript 2 a2 to as3 can be tough, and this forum will show the road to as3. as3 is not best suited for small animation or web projects, and is in someways a red headed stepchild of java and c++. But, it still has a purpose, for now.
Post Reply
darknkreepy3#
Site Admin
Posts: 253
Joined: Tue Oct 27, 2009 9:33 pm

automating nested arrays with split

Post by darknkreepy3# »

here is a great way to not have any hardcoded lengthily approaches to nested arrays. it will serve a good measure to see how nested arrays are hand coded, but this allows for a much more dynamic approach to arrays that are nested within other arrays.

Code: Select all

/*
07-25-2010 nested_arrays.fla v1.0 by Kristoffe Brodeur. ©2010 All Rights Reserved

a great way to make nested arrays, much more flexible than by hand coding
you can even get a php output, etc, and have it spit into a LoadVars into the winStr if ever needed
*/

var arrBase=new Array();
/*
-----old way of doing long nested arrays
private var _combos:Array=new Array(
new Array(0, 1, 2), new Array(3, 4, 5), new Array(6, 7, 8),  
new Array(0, 3, 6), new Array(1, 4, 7), new Array(2, 5, 8),  
new Array(0, 4, 8), new Array(2, 4, 6)
); 
-----
*/

//now, I make a string of number groups seperated by say '^' and the numbers inside by ','
var arrWinStr:String="0,1,2^3,4,5^6,7,8^0,3,6^1,4,7^2,5,8^0,4,8^2,4,6";
//split breaks a string by a character and makes an array of the result, throwing out the seperator
var winArr:Array=arrWinStr.split("^");
//now with groups of strings in an array structure, we can then loop through and put them into _combos
var _combos:Array=new Array();
//it is always best to make an integer to count something and then use that variable in the for loop
var len:int=winArr.length;
//this makes sense in thousands of loops. it doesn't have to count that part of memory, it just sees len
trace(len);

//
for(var a=0;a<len;a++)
	{
	//now we can get winArr[*] and split it up one at a time into a new array split by ','
	_combos[a]=winArr[a].split(",");
	//8 main 'a' loops 0~7, and each _combos[0~7] has 3 array elements
	trace(_combos[a]);
	}
Post Reply