13

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?

BefittingTheorem
  • 10,459
  • 15
  • 69
  • 96
  • +1. Very good question, what are your observations? I'm curious! – dirkgently Mar 06 '09 at 14:18
  • 1
    It's a good fundamental one eh :) I had a bug in project a while back, which I traced to an array being either reversed or the for-each was moving through it back to front. I replaced the for-each with a for-index++ lopp and the bug disappeared. I never actually made 100% sure what the cause was. – BefittingTheorem Mar 06 '09 at 14:26

3 Answers3

8

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

Click here for Colin Moock's words from his book.

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

thirtydot
  • 224,678
  • 48
  • 389
  • 349
Brian Hodge
  • 2,125
  • 2
  • 19
  • 29
  • 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? – BefittingTheorem Mar 07 '09 at 09:18
  • 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 Mar 08 '09 at 06:48
  • 2
    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 – BefittingTheorem Mar 09 '09 at 09:03
  • Colin Moock's book says, "In most cases, the order in which for-each-in and for-in loops enumerate an object's variables is not guaranteed." NOTE: the usage of "object's variables"... Would an object's members be considered its "variables"? The book also says order varies across different versions of Flash runtimes, which makes me wonder if this issue has been remedied. – Pup Nov 27 '10 at 00:30
5

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

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
  • 108,024
  • 16
  • 131
  • 187
  • The question is whether this code produces the same output for different Flash runtimes. – Pup Nov 27 '10 at 00:31