[HaXe AS3][SOLVED] How to Iterate Backwards Through an Array?

Subscribe to [HaXe AS3][SOLVED] How to Iterate Backwards Through an Array? 6 posts

avatar for zero579911 zero579911 429 posts
Flag Post

Hi, this seems like a simple question, but I can’t seem to find the answer on google. In AS3 you could do for (var i:int = a.length - 1; i >= 0; --i) { } but in HaXe, you can only do for (i in 0 ... a.length) { } and this only iterates forward.

Thanks.

 
avatar for mikebolt mikebolt 94 posts
Flag Post

Completely guessing here:
for (i in a.length … 0) { }

or a while loop

 
avatar for zero579911 zero579911 429 posts
Flag Post
Originally posted by mikebolt:

Completely guessing here:
for (i in a.length … 0) { }

or a while loop

That doesn’t work.
EDIT: Thanks to VBCPP (in GDR) for showing me the link to the answer: http://haxe.org/doc/snip/countdownintiter You were close though, mikebolt.

 
avatar for someone93 someone93 261 posts
Flag Post

Would something like this work?

for (i in 0 ... a.length) {
    n = a.length - i - 1;
    // Do stuff with index n
}

or

n = a.length - 1;

for (i in 0 ... a.length) {
    // Do stuff with index n
    n--;
}

Edit: Sorry, didn’t see your edit! …was it even marked as solved when I started reading this topic? :S

 
avatar for qwerberberber qwerberberber 508 posts
Flag Post

while(i—>0)

 
avatar for player_03 player_03 1249 posts
Flag Post

Honestly, I’d suggest using qwerber’s solution instead. You shouldn’t have to invert i every time you want to access it.