‎2020 Nov 10 1:32 PM
Hey everyone,
is there a simpler/better way to make a nested counter loop than this?
DATA(XCOUNT) = 0.
DATA(YCOUNT) = 0.
DO X TIMES.
XCOUNT = XCOUNT + 1.
DO Y TIMES.
YCOUNT = YCOUNT + 1.
DO=>SOMETHING( X = XCOUNT Y = YCOUNT ).
ENDDO.
YCOUNT = 0.
ENDDO.
XCOUNT = 0.
‎2020 Nov 11 10:45 AM
Your code is so simple to understand that I don't see why you want to improve it. The only weird thing is common to many developers, is the "reset after execution" ("?count = 0"), as it's prone to errors and can be generally avoided. Instead, you should always prefer the "reset before execution" (move DATA(YCOUNT) = 0 before DO Y TIMES, and remove the 2 lines ?count = 0).
But if it's for the fun/NOT a productive code, you may declare DO=>SOMETHING with a dummy returning parameter and do like this (I use ASSERT here, but I could use anything else, it's just to say that the result is always 1 and has no importance for the objective):
ASSERT 1 = REDUCE i( INIT i = 1
FOR xcount = 1 THEN xcount + 1 WHILE xcount <= x
FOR ycount = 1 THEN ycount + 1 WHILE ycount <= y
LET dummyresult = DO=>SOMETHING( X = XCOUNT Y = YCOUNT ) IN
NEXT i = 1 ).WARNING: stunt[wo]men, don't try this at home and at work!
‎2020 Nov 10 1:40 PM
Hello Former Member
From SAP Help (https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abapdo.htm): SY-INDEX always refers to the current LOOP
Which means you could do something like:
DATA(XCOUNT) = 0.
DO X TIMES.
XCOUNT = XCOUNT + 1.
DO Y TIMES.
DO=>SOMETHING( X = XCOUNT Y = sy-index ).
ENDDO.
ENDDO.
XCOUNT = 0.You could also store X count in the class:
DO x TIMES.
do=>set_x( sy-index ).
DO y TIMES.
do=>something( y = sy-index ).
ENDDO.
ENDDO.
METHOD something.
WRITE: / do=>x.
WRITE: / y.
ENDMETHOD.Kind regards,
Mateusz
‎2020 Nov 10 7:15 PM
Not fundamentally, but xcount and ycount are clearly helper variables and won't have use outside of the code you've posted, so something like:
DATA(XCOUNT) = 0.
DO XCOUNT TIMES.
DATA(YCOUNT) = 0.
XCOUNT = XCOUNT +1.
DO YCOUNT TIMES.
YCOUNT = YCOUNT + 1.
DO=>SOMETHING( X = XCOUNT Y = YCOUNT ).
ENDDO.
ENDDO.or
DATA(XCOUNT) = 0.
DO XCOUNT TIMES.
XCOUNT = XCOUNT + 1.
yloop( XCOUNT ).
ENDDO.
METHOD yloop.
DATA(YCOUNT) = 0.
DO YCOUNT TIMES.
YCOUNT = YCOUNT + 1.
DO=>SOMETHING( I_X = I_X I_Y = YCOUNT ).
ENDDO.
ENDMETHOD.
‎2020 Nov 11 10:45 AM
Your code is so simple to understand that I don't see why you want to improve it. The only weird thing is common to many developers, is the "reset after execution" ("?count = 0"), as it's prone to errors and can be generally avoided. Instead, you should always prefer the "reset before execution" (move DATA(YCOUNT) = 0 before DO Y TIMES, and remove the 2 lines ?count = 0).
But if it's for the fun/NOT a productive code, you may declare DO=>SOMETHING with a dummy returning parameter and do like this (I use ASSERT here, but I could use anything else, it's just to say that the result is always 1 and has no importance for the objective):
ASSERT 1 = REDUCE i( INIT i = 1
FOR xcount = 1 THEN xcount + 1 WHILE xcount <= x
FOR ycount = 1 THEN ycount + 1 WHILE ycount <= y
LET dummyresult = DO=>SOMETHING( X = XCOUNT Y = YCOUNT ) IN
NEXT i = 1 ).WARNING: stunt[wo]men, don't try this at home and at work!