For..in vs for

You would these two would work in a similar fashion right? Well I wasted over 4 hours yesterday because they don’t! :fight:

var randomArray:Array = new Array(10);
var numList:Array = new Array();
 
for (var i:String in randomArray)
 numList.push(i);

You would think tracing numList would output:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Right?

Wrong! for…in seems to be a LIFO structure instead of a FIFO structure. It outputs the following:

9, 8, 7, 6, 5, 4, 3, 2, 1, 0

After 4 hours of confusion I resorted to using two for…in loops (one to get data and one to reverse the mess the first one makes). Today I decided to see if that’s the way a for loop works (because I was sure it didn’t) and when I got my answer I felt like personifying Flash and kicking it around!

:hitman:

Needless to say I have now converted all of my for…in loops to for loops.

Can someone please explain to me why the loops are different in such a way. Oh, the loops are being run from within an external class just in case it makes a difference.