20 Replies Latest reply on Feb 26, 2008 8:57 PM by rlc5611

# MultiDimensional array

I am trying to create an array that holds some info for each item in the array how would I do that?
I want to add a two variables to an array that are essentialy one item in the array. Also how would I acess them later?
a= 1;
b = 2;
myArray = [];
myArray.push [??? this is were I confused with the syntax.
I would really appreciate the help.
Thanks
• ###### 1. Re: MultiDimensional array
Oh I forgot, is there a way to sort the array by values least to greatest?
Thanks again.
• ###### 2. Re: MultiDimensional array
Push, places an item in an array, at the end of the array.

If you want to add an array to another array, you push it like any other
value:

var a = [];
var b = [1,2];
a.push(b);

trace(a[0][1]);

traces 2 - the 2nd item (index 1) in the 1st item (index 0)

You can push an object too:

var a = [];
var b = {a:1, b:2};
a.push(b);

trace(a[0].b)

traces 2 also.

HTH

--
Dave -
http://www.blurredistinction.com

• ###### 3. Re: MultiDimensional array
PS - look up the Array class - there's a sort method you can use. With a
multidimensional array though, you'll need to use the compareFunction.

--
Dave -
http://www.blurredistinction.com

• ###### 4. Re: MultiDimensional array
:

• ###### 5. Re: MultiDimensional array
myArray[0] will be the first element (=1) pushed into your array, and myArray[1] will be the 2nd element (=2) pushed into your array.
• ###### 6. Re: MultiDimensional array
Yeah the thing is that I have two variables
var time
var x
I want them to be associated together so that I can find the x and then find the time for that x. Then they need to be ordered from least time to most time. What method would I use to accomplish that?
• ###### 7. Re: MultiDimensional array
create objects and add all your variables as properties of those objects and then push your objects into the array.
• ###### 8. Re: MultiDimensional array
Srry for relying on you guys so much but Im not sure how to do this.
This is what I came up with.
nextBoom = [];
tempob = {};
tempob.time = time;
tempob.x = x;
nextBoom.push (tempob);
trace(nextBoom);
Except is not tracing anything. What am I doing wrong?
Really Appreciate the help!

• ###### 9. Re: MultiDimensional array
Srry copied it wrong. This is what I really had for the push.
nextBoom.push (tempob);
• ###### 10. Re: MultiDimensional array
What you say is not a true statement. It is, in fact, tracing the object proving that the object is, in fact, in the array.

What you need to do is use correct syntax to acces it. For example:

(oh and stop using keywords for variable names unless that is what you are really trying to do);

• ###### 11. Re: MultiDimensional array
>>nextBoom.push (tempob);

That's not going to work. The push() method of arrays is just that - a
method, you don't use array access notation with it.

What you want is:

nextBoom.push(tempob);

--
Dave -
http://www.blurredistinction.com

• ###### 12. Re: MultiDimensional array
Just a quick clarification for everyone reading this in the web forums, and not via a newsreader (where I presume it works fine). You cannot see the array access operator in the above posts because it is interpreted as a 'bold' formatting command for subsequent content in that post, so that's why some people's reponses omit the correction and why Dave's response above seems out of place (Dave's answer is - of course - correct, but is not apparent when viewing this thread via the web, because you cannot see what he is referring to).
If you are posting something with an array access operator example, using the variable i as the iterator, and are (for some reason) not using the attach code button for the web... then please do so with an extra space before the i, as in: [ i]. Otherwise web users will see your post go bold...and not see the array access operator. It seems easier to ask people to do that than to ask them to use j,lol.
• ###### 13. Re: MultiDimensional array
nextBoom.push(tempob);
Sorry if Im just missing something obvious, but whats the differance between what I had and that?
>>What you want is:
>>nextBoom.push(tempob);
Ok so now I want to arrange the array so that the entire array is arranged by the value of time in each of the objects of the array. How would I do that?
Thanks

• ###### 14. Re: MultiDimensional array
Oh and Im using flash mx if that makes a differance. Ive been searching the documents but have only found stuff that is to newer versions to flash.
• ###### 15. Re: MultiDimensional array
I can't remember what part of sortOn doesn't work in Flash 6 but it does have fewer options than Flash 7 or 8. I can't remember if the Array.NUMERIC will take in Flash 6. If not, you can make your own sorting function.
• ###### 16. Re: MultiDimensional array
I'm not really sure that sortOn will work for you in Flash 6 since everything in an array is treated as text. I am also a lousy sorter and only know how to do a bubble sort and I can only do that on keypunch cards in Fortran 4. So a quick and dirty way of sorting your multidimensional array for numerical time using the Flash sortOn would be to temporarily pad your times with leading zeroes where required, do the sort and then remove those zeroes after the sort is done. (This does assume that there are no negative times.)

There may be a better way but I'm betting that Flash can sort more efficiently than I can.

edited to say that the sortOn options added in Flash 7 was one of my favorite feature improvements.
• ###### 17. Re: MultiDimensional array
That previous script didn't work well on integers. This script works both with and without decimal places in the "number". Using Number() on a string number with leading zeros is probably not a good thing to do anyway.
• ###### 18. Re: MultiDimensional array
And here is my inefficient bubble sort. I'm sure that kglad has a brilliant sort because he's great at math

BTW the clunky script above that pads the extra zeroes in front took 16 milliseconds to complete on my PC while the bubble sort script here took 62 milliseconds. The advantage of the bubble sort is that it will sort anything correctly and handle negative numbers. I know better sorting methods than a bubble sort exist but I've never had to use them and, since Flash 7 came out, there has been no need to look. I also checked the sortOn with the Array.NUMERIC option (available in Flash 7) and it took 15 milliseconds.
• ###### 19. Re: MultiDimensional array
You guys have no idea how many headaches you prevented me from having and how many you relieved from me at the same time. I cant tell you how much I appreciate the help.
BTW how do you guys tell how long some script took?
• ###### 20. Re: MultiDimensional array
For what it is worth, I googled efficient sorting methods and I could reasonably understand a heap sort so I scripted that and got the sort time down to 15 milliseconds. This will work in Flash 6.