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

6 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.

 
Flag Post

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

or a while loop

 
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.

 
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

 
Flag Post

while(i—>0)

 
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.