Hi Martjin,

sorry - but I have to disappoint you. Every language that I know, that has a "for-each" construct, assigns a copy of the actual array element to the loop variable.

If you want to modify arrays, you have to use the indexed form of FOR:

for index = 1 to %elem(array);
...
end-for;

If you define "index" as uns(20) you should be able to numerate every array element.

Interesting question would be the question - does the RPG compiler and runtime guarantee that for-each is iterating for 1 to %elem()?

That question would be interesting, because other languages do not give any guarantee that "for-each" iterates the array elements in any particular order.

HTH
Daniel


Am 09.07.2024 um 11:15 schrieb Martijn van Breden <m.vanbreden@xxxxxxxxxxxxxxxxxxxxxxxxxx>:

Hi,

I'm building some functionality that relies on several arrays that need to be filled, checked and modified on the fly. I like the for-each loop with dim(*auto: nn) for this. In the initial load proces I can use '*next' as an index, which is great.
I couldn't find a way to get the current iteration index in a subsequent loop without holding track of it myself.

In simplified pseudo code:

"elements" has been filled in a previous loop and now needs to be updated.

for-each element in elements;
elemIndex += 1;
// do some checking
element.checked = *on;
// update element data
element.data = 'different';
elements(elemIndex) = element;
endfor;

I would like to get rid of elemIndex field and use something like this

elements(*current) = element

I couldn't find anything on this, except for questions on StackOverflow with people asking similar questions about other languages.

Am I overlooking something?


Kind regards



Martijn van Breden

lead software architect

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.