0

I posted my problem a few hours ago, but I think I figured out how to ask my question in a more comprehensible way.

This is my code:

// 1. Intro
var introPL1:Array = ["intro1","intro2","intro3","intro4"];
var introPL2:Array = ["intro5","intro6","intro7","intro8","intro9"];
var introPL3:Array = ["intro10","intro11"];
var introPL4:Array = ["intro12","intro13"];
var allIntro:Array = [introPL1,introPL2,introPL3,introPL4];
// 2. Clothes
var clothesPL1:Array = ["clothes1","clothes2","clothes3","clothes4","clothes5"];
var clothesPL2:Array = ["clothes6","clothes7","clothes8"];
var clothesPL3:Array = ["clothes9","clothes10"];
var clothesPL4:Array = ["clothes11","clothes12","clothes13"];
var allClothes:Array = [clothesPL1,clothesPL2,clothesPL3,clothesPL4];
// 3. Colored Numbers
var colNumPL1:Array = ["colNum1","colNum2","colNum3","colNum4","colNum5"];
var colNumPL2:Array = ["colNum6","colNum7","colNum8"];
var colNumPL3:Array = ["colNum9","colNum10"];
var colNumPL4:Array = ["colNum11","colNum12","colNum13"];
var allColNum:Array = [colNumPL1,colNumPL2,colNumPL3,colNumPL4];

var allStuff:Array;
allStuff = allIntro.concat(allClothes, allColNum);
trace(allStuff[4]);

When I trace allStuff[4] it displays "clothes1,clothes2,clothes3,clothes4,clothes5". The thing is, I would like all the stuff to be in the allStuff array (without sub-arrays) and when I trace allStuff[4], I would like it to display "intro5" (the fifth item in the huge allStuff array).

2 Answers 2

2

the function you want to use then is concat

here's the example from adobe

var numbers:Array = new Array(1, 2, 3);
var letters:Array = new Array("a", "b", "c");
var numbersAndLetters:Array = numbers.concat(letters);
var lettersAndNumbers:Array = letters.concat(numbers);

trace(numbers);       // 1,2,3
trace(letters);       // a,b,c
trace(numbersAndLetters); // 1,2,3,a,b,c
trace(lettersAndNumbers); // a,b,c,1,2,3

it's pretty straight forward:

allStuff= allStuff.concat(introPL1,introPL2,introPL3,introPL4,clothesPL1,clothesPL2,clothesPL3,clothesPL4,colNumPL1,colNumPL2,colNumPL3,colNumPL4);

you could also do a

allStuff = []
for each(var $string:String in $arr){
   allStuff.push($string)
}

for each array, or make it into a function

Sign up to request clarification or add additional context in comments.

1 Comment

this doesn't help me at all, bro. I have already checked out the "help" from adobe. and it sux. could you please help me using an example from the code I provided here?
0

Okay, once you have declared your arrays like so, you need an additional operation to flatten your arrays allClothes and so on. Do like this:

function flatten(a:Array):Array {
    // returns an array that contains all the elements 
    // of parameter as a single array
    var b:Array=[];
    for (var i:int=0;i<a.length;i++) {
        if (a[i] is Array) b=b.concat(flatten(a[i]));
        else b.push(a[i]);
    }
    return b;
}

What does it do: The function makes an empty array first, then checks the parameter member by member, if the i'th member is an Array, it calls itself with that member as a parameter, and adds the result to its temporary array, otherwise it's just pushing next member of a into the temporary array. So, to make your allIntro a flat array, you call allIntro=flatten(allIntro) after declaring it as you did. The same for other arrays.

3 Comments

hi Vesper - I don't know why but when I do this what you've suggested here, when I trace the array.length - my arrays are always one item longer (a phantom item perhaps) than they should be.
If you trace the arrays themselves, where is the "phantom item" located? There should be none, but if the source array had a null item, then it'll remain in the result. Check inputs please.
I tried to search for it - couldn't find it. To be honest I'm an AS3 beginner and reading the code someone else writes is a bit of an enigma cracking for me. So I found another solution and now it (somehow) works just right :) Thanks for the help anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.