Daniel, thanks for providing those insights! I do have a few
additional comments though:

On Tue, Jul 9, 2024 at 5:25 AM Daniel Gross <daniel@xxxxxxxx> wrote:

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.

Indeed, that is the point of "for-each", and it's why there is even
terminology that is distinct from simply "for".

But Python, and perhaps a growing number of other languages, do make
it easy to get both the index and the element value in the same loop.

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

for index = 1 to %elem(array);

This is the classic idiom, and any time you need the array index, this
is the standard and usually best way to do it. (In most languages, not
just RPG).

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.

What languages are you thinking of? I don't know of any that would
iterate *an array* out of order. There are some data structures that
are not intrinsically ordered (such as sets or hash maps), and using
the language-appropriate "for-each" over those might not follow the
desired order. RPG doesn't have any such data structures. Because of
the low-level notion of what an array is (contiguous, evenly spaced
addresses in memory), the simplest thing to do when implementing
"for-each" in the compiler is to iterate an array in index order.
Anything else would be simultaneously more complex to implement,
slower to execute, and less advantageous for program logic.

John Y.

As an Amazon Associate we earn from qualifying purchases.

This thread ...


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.