For each ... next

While I find the iteration construct quite useful, often I need to iterate a list in reverse order. Is this possible, or is there no choice but to do this using the FOR … DOWNTO… NEXT construct ?

Or should this be posted as a feature request ?

From the LR:

Use To to increment the counter at every loop iteration by the Step value (default: 1). Use DownTo to subtract the Step value from the counter at every iteration. You can also count down by using a negative Step value when using To as described in the “Step value” section below.

There is no for each backwards construct in the language.
You have to make a normal for loop running backwards.

PS: If you loop over an array, you could reverse the order of objects there.

PPS: Or loop forward, but access items with (items.ubound-i) instead of (i).

have a look here and reverse the order insinde

1 Like

Yes I’m well aware of several options.

My issue is specifically with iterating over the values of a dictionary which appear to be returned in Last-In-First-Out (LIFO) order, and I’m frequently having to reverse this to get them into FIFO order. I can think of at least 5 ways to do this, but they’re all slower than if the dictionary values could be iterated in FIFO order.

While this might not seem to matter to some, it starts to hurt if the dictionary has 100,000 entries. It’s positively painful at a million.

The order of keys in a dictionary is not defined. Don’t relay on it.
Better keep an array of keys in order for that.

1 Like

Hehe Christian, I have indexes thoroughly sorted.

@MarkusR thank you, very much - I didn’t know Xojo could do that - I know EXACTLY what to do with it :slight_smile:

2 Likes