views:

4037

answers:

3

I would like to know the iteration order for the Array, Dictionary and Object types in AS3, for both the for-each and the for-in loops. Also what factors can change the iteration order of these loop type combinations?

For example I presume that using a for-each on an Array type always move from the first element to the last. For-each cannot be used on a Dictionary so how is the order determined using a for-in loop?

+2  A: 

Do you mean the for (x in y) type of for-each loop? The AS3 specification says that for loops "have the same syntax and semantics as defined in ECMA-262 edition 3 and E4X".

ECMA-262, section 12.6.4 state that "the order of enumeration is defined by the object" - in other words, it will depend on whether you're iterating through a list, a dictionary, an array etc.

I would hope that the documentation for most collections will explicitly state the enumeration order (or that the order isn't defined) but I haven't checked...

EDIT: Unfortunately the specs state that "the mechanics of enumerating the properties [...] is implementation dependent." I can't see anything in the Array docs to specify ordering. It's a bit unsatisfactory, to be honest :(

Jon Skeet
Thanks Jon, I just made the question more specific based on your answer
Brian Heylin
I completely rephrased the question, I think that's quite clear now :)
Brian Heylin
Thanks for the edit Jon, It certainly is unsatisfactory that it is not specified in the documentation. I'll take a look for AS3 specific documentation on this
Brian Heylin
+2  A: 

The order of evaluation changed in AS3.0 Earlier, in AS 1.0 and 2.0, the order of evaluation was based on the order in which the objects were added. Now, in 3.0, all looping maintains array order. Try the snippet:

var a:Array = new Array();
a[ 1 ] = 'first';
a[ 0 ] = 'second';

for (var key:String in a) 
    trace( key + ': ' + a[ key ] );

Try the above with AS3.0 and AS1.0/2.0

dirkgently
+3  A: 

As stated in Essential Actionscript 3.0 by Colin Moock, the order of a for each in loop is not guarenteed unless enumerating an XML or XMLList object;

Click here for Colin Moocks words from his book.

There are work arounds as discussed here, but honestly if you need to guarentee the order then just use a regular old for loop and have it iterate (length, numChildren, etc.) number of times.

Brian Hodge hodgedev.com blog.hodgedev.com

Brian Hodge
is there any particular reason you know for the order not being guaranteed? I can understand because an Array can have non-numeric indexing. Which leads me to wonder.. surely a Vector.<> has a guaranteed order since it is stored sequentially in memory?
Brian Heylin
As I stated above, Colin Moock covered this in detail, feel free to follow the link above which is taken directly from his book. -Cheers
Brian Hodge
To be honest I am quite uncomfortable with that statement if no reason is given. Not that I believe it to be false, but that there is no logic or reasoning behind it (most likely due to editing). I'm interested in the internal mechanics of why a for each loop cannot guarantee order on an Array
Brian Heylin