10-18-2012 9:40 AM
Hi,
I have a scenario where I have an outer loop (a WHILE loop)and within that, an inner loop at a checktable. If I find the currently used value (used by the outer loop) anywhere in the checktable, I want to directly exit the current iteration of the outer loop, increase it's counter and continue with the next. Can anybody tell me how I could do that?
Currently I am employing a workaround - in the IF clause that causes to EXIT the inner loop as soon as the value has been found, I set a flag_variable. After exiting (thus returning to the outer loop) I query that variable and when it has a certain value, I use the CONTINUE command to leave the current iteration of the outer WHILE_loop.
That is working - at least I guess so, I haven't tested the thing yet because the entire construction is immensely complex and not yet finished. However, I would like to do that more elegantly with a single command if that were possible?
Thanks a lot!
Best regards,
Sapperdapper
10-18-2012 10:03 AM
Hello Friedrich,
have you considered about using "Gruppenstufenverarbeitung" - I like this german buzz-word 😉 - a.k.a Control Level Processing with AT..ENDAT?
You could write it like this:
LOOP AT firsttab ASSIGNIGN <s_tab1>.
AT NEW keyfield1.
* READ CheckTable AND continue when new line has to be processed
ENDAT.
* Go on with existing processing.
...
ENDLOOP.
Kind regards,
Hendrik
10-18-2012 9:45 AM
Hello Sapperdapper ,
If you dont mind Can you please place your code?
10-18-2012 9:51 AM
Hi Rajesh,
yes - I will just post the part of the whole which is of relevance here to avoid creating more confusion than all:
WHILE Tabctr_curr2 <= Tabctr.
READ TABLE Tables_tab INTO wa_Tables INDEX Tabctr_curr2.
Tab2 = wa_Tables-TABNAME.
* In case the Tab2 we have selected just now is the same as
* Tab1, we do not have to consider it further, but we can
* just leave this iteration of the current loop and go on
* to the next.
IF Tab2 = Tab1.
ADD 1 TO Tabctr_curr2.
CONTINUE.
ENDIF.
* If Tab2 is different from Tab1, we can now check whether
* a) there are at least two tables in our Checktable and
* b) Tab2 is already there.
* If BOTH are true, we leave the current iteration of the loop.
* First we check the line_ct (of the checktable) because
* if it is 1, we don't even have to check it further, in
* that case there can't be any relations yet.
IF line_ct >= 2.
* If this first condition is true, we loop through the table.
LOOP AT Checktable INTO wa_Checktable.
* If we find the current value of Tab2 already processed...
IF wa_Checktable-TABNAME = Tab2.
* we exit this loop altogether.
EXIT.
ENDIF.
ENDLOOP.
* Before the IF statement is closed, we set a variable that will
* allow us to decide whether to leave the current iteration of the
* Tab2_loop and continue with the next (another Tab2).
* In case the IF block is not executed, line_ct=1, so there can't be
* any relation processed yet, so the case where we want to leave the
* current iteration of the Tab2_loop is not possible.
Tabfound = 'X'.
ENDIF.
* Now we check Tabfound and if it reads 'X'...
IF Tabfound = 'X'.
* ... we leave the current iteration of the Tab2_loop and continue
* with the next. (This is a Workaround I currently use because I don't
* know how, from the inner loop (LOOP AT Checktable) to directly leave
* the current iteration of the outer.
ADD 1 TO Tabctr_curr2.
CONTINUE.
ENDIF.
Thanks a lot!
Best regards,
Sapperdapper
10-18-2012 10:06 AM
Hi Sapperdapper,
PLease check the below code
WHILE Tabctr_curr2 <= Tabctr.
READ TABLE Tables_tab INTO wa_Tables INDEX Tabctr_curr2.
Tab2 = wa_Tables-TABNAME.
* In case the Tab2 we have selected just now is the same as
* Tab1, we do not have to consider it further, but we can
* just leave this iteration of the current loop and go on
* to the next.
IF Tab2 = Tab1.
ADD 1 TO Tabctr_curr2.
CONTINUE.
ENDIF.
* If Tab2 is different from Tab1, we can now check whether
* a) there are at least two tables in our Checktable and
* b) Tab2 is already there.
* If BOTH are true, we leave the current iteration of the loop.
* First we check the line_ct (of the checktable) because
* if it is 1, we don't even have to check it further, in
* that case there can't be any relations yet.
IF line_ct >= 2.
* If this first condition is true, we loop through the table.
LOOP AT Checktable INTO wa_Checktable.
* If we find the current value of Tab2 already processed...
IF wa_Checktable-TABNAME = Tab2.
* we exit this loop altogether.
EXIT.
ENDIF.
ENDLOOP.
* Before the IF statement is closed, we set a variable that will
* allow us to decide whether to leave the current iteration of the
* Tab2_loop and continue with the next (another Tab2).
* In case the IF block is not executed, line_ct=1, so there can't be
* any relation processed yet, so the case where we want to leave the
* current iteration of the Tab2_loop is not possible.
else. " if your line_ct < 2.
Tabfound = 'X'." Comment this
ADD 1 TO Tabctr_curr2. "Add this line
CONTINUE. " Add this line
ENDIF.
* Now we check Tabfound and if it reads 'X'...
IF Tabfound = 'X'. " Comment this
* ... we leave the current iteration of the Tab2_loop and continue
* with the next. (This is a Workaround I currently use because I don't
* know how, from the inner loop (LOOP AT Checktable) to directly leave
* the current iteration of the outer.
ADD 1 TO Tabctr_curr2. "Comment this
CONTINUE. "Comment this
ENDIF. "Comment this
10-18-2012 10:02 AM
Hi,
To my knowledge, there is no such command that would satisfy your requirement. By the way i think your workaround is good enough to handle this requirement.
Regards,
Xavier
10-18-2012 10:03 AM
Hello Friedrich,
have you considered about using "Gruppenstufenverarbeitung" - I like this german buzz-word 😉 - a.k.a Control Level Processing with AT..ENDAT?
You could write it like this:
LOOP AT firsttab ASSIGNIGN <s_tab1>.
AT NEW keyfield1.
* READ CheckTable AND continue when new line has to be processed
ENDAT.
* Go on with existing processing.
...
ENDLOOP.
Kind regards,
Hendrik
10-18-2012 10:14 AM
Hi all,
thanks for all the help!
@ Hendrik
I will have a look at that concept in due time. The first step is getting it to work at all, the second will be optimizing it as far as possible.
Best regards,
Sapperdapper
10-18-2012 11:51 AM
Does this help.
data:iv type i,lv type matnr.
data:it type sorted table of mara with unique key matnr.
data:wa type mara.
lv = 'ABC'.
iv = 10.
select * from mara into table it up to 9 rows.
while iv > 10.
add 1 to iv.
loop at it into wa where matnr = lv.
endloop.
check sy-subrc = 0.
endwhile.
10-18-2012 1:53 PM
Hi Kesavadas,
honestly I don't know if it is helpful 😉 The thing is so complex that I will have to think over it. Thanks in any case!
I've made it now, so for now the task is done - optimizing will be the next step, but no less important for that - the number of tables, currently four, will be much bigger in real life, so it will take much longer to run and every little thing I can tweak to make it smoother will help.
Best regards,
Sapperdapper