Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

LOOP in LOOP Problem

rahul2000
Contributor
0 Likes
1,213

Dear all...

I have a code which is like this..

LOOP AT ITAB1.

LOOP AT ITAB2 WHERE BELNR = ITAB1-BELNR

w_index = w_index + 1.

.

.

.

.

MODIFY ITAB1 INDEX W_INDEX TRANSPORTING SOMETHING.

ENDLOOP.

ENDLOOP.

Now what i want is that suppose while looping ITAB1 for the first time doc no(belnr) 12345 comes and loop at itab2 gets executed...

Now after this inner loop is completed and in ITAB1 again if doc no(belnr) 12345 comes again,then the inner loop LOOP AT ITAB2 should not get executed again until some other doc no other than 12345 comes again...

if this problem is solved then my index problem for modifying will get resolved i guess.

1 ACCEPTED SOLUTION
Read only

venkat_o
Active Contributor
0 Likes
1,192

Hi Rahul But, If your ITAB1 can have multiple same documents numbers, before loop and endloop, SORT itab1 by belnr. Samething at ITAB2. Then write code like this.

data:l_first_belnr type c.
LOOP AT ITAB1.
    at new belnr.
     l_first_belnr = 'X'.
     endat.
    if l_first_belnr = 'X'.
         LOOP AT ITAB2 WHERE BELNR = ITAB1-BELNR
            w_index = w_index + 1.
            .
            .
            .
            .
           MODIFY ITAB1 INDEX W_INDEX TRANSPORTING 
           SOMETHING.
         ENDLOOP.
   clear l_first_belnr.
  endif.
ENDLOOP.
I hope that helps you. Regards, Venkat.O

10 REPLIES 10
Read only

Former Member
0 Likes
1,192

instead of writing two loop statements loop one itab and then write read statement and then use modify statement

Read only

Former Member
0 Likes
1,192

Hi,

get the ITAB1 belnr in to a variable.

and in the inner loop check the value .

if it is same write continue statement.

u need to every time in the sense for every iteration of ITAB1 store the new belnr in the variable.

reward if helpful

raam

Read only

venkat_o
Active Contributor
0 Likes
1,193

Hi Rahul But, If your ITAB1 can have multiple same documents numbers, before loop and endloop, SORT itab1 by belnr. Samething at ITAB2. Then write code like this.

data:l_first_belnr type c.
LOOP AT ITAB1.
    at new belnr.
     l_first_belnr = 'X'.
     endat.
    if l_first_belnr = 'X'.
         LOOP AT ITAB2 WHERE BELNR = ITAB1-BELNR
            w_index = w_index + 1.
            .
            .
            .
            .
           MODIFY ITAB1 INDEX W_INDEX TRANSPORTING 
           SOMETHING.
         ENDLOOP.
   clear l_first_belnr.
  endif.
ENDLOOP.
I hope that helps you. Regards, Venkat.O

Read only

0 Likes
1,192

Hi Vinod,

Thanks a ton...

I will be getting back to work on tuesday...

Looking at u r solution...my guess is that it will work...

Will BELNR have to be the first field in both my internal tables??

Thanks a ton...

Dear all,

I will surely reward each and everyone..

Thanks a ton..

Will get back to u on tuesday

Read only

0 Likes
1,192

HI Rahul,

U R Right. Belnr should be the first field of both tables. But It is not like AT EVENT Field should always be the first field. Suppose u have belnr as second field of ur internal table and if u use AT NEW belnr then when ever ur first field or belnr changes then control will enter in to this block. i.e. It will take till the field in AT event as key. If any field with in the key changes then control will enter. Hope u understood how AT EVENT works. Another thing is SORT is Mandatory for AT EVENTS.

Thanks,

Vinod.

Read only

Former Member
0 Likes
1,192

Hi,

If you cant make the field BELNR as the first field go like this....

data : W_BELNR TYPE BELNR.

SORT: itab1 BY belnr,

itab2 BY belnr.

LOOP AT ITAB1 .

IF ITAB-BELNR <> W_BELNR.

LOOP AT ITAB2 WHERE BELNR = ITAB1-BELNR

ENDLOOP.

ENDIF.

W_BELNR = ITAB1-BELNR.

ENDLOOP.

Read only

0 Likes
1,192

Hi Vinod,

Yup i did understand hot these AT events work.

Thanks a ton.

Will get back to you

Cheers

Read only

vinod_vemuru2
Active Contributor
0 Likes
1,192

Hi Rahul,

Check below the effective way to achieve ur requirement.

Mandatory to use AT NEW event in this case is BELNR

should be first field of the tables.

SORT: itab1 BY belnr,

itab2 BY belnr.

LOOP AT ITAB1 INTO wa_tab1.

CLEAR itab1.

itab1 = wa_tab1.

AT NEW belnr.

READ TABLE itab2 WITH KEY belnr = itab1-belnr

BINARY SEARCH

TRANSPORTING NO FIELDS.

CHECK sy-subrc IS INITIAL.

CLEAR w_index1.

w_index1 = sy-tabix.

To reduce number of loop passes loop from the index.

If we use where clause then full table scan will happens.

LOOP AT ITAB2 FROM w_index1.

w_index = w_index + 1.

.

.

.

.

MODIFY ITAB1 INDEX W_INDEX TRANSPORTING SOMETHING.

ENDLOOP.

ENDAT.

ENDLOOP.

Thanks,

Vinod.

Read only

Former Member
0 Likes
1,192

hi ,

u can use the read index statement at the first loop or u can use the count ..

if count = something..

exit..

else.

something..

endid.

like that..

regards,

venkat.

Read only

Former Member
0 Likes
1,192

Hi,

Pls try to use some flag in ur code and try to fix it.