Application Development 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: 

Increasing the index of the loop.

former_member1360394
Participant
0 Kudos


Hello Everyone,

I have a loop which I write like below:

Loop at itab into wa from index.

I use parallel cursor and use another loop.

     loop at itab1 into wa1.

          **** my code *****

    endloop.

endloop.

Now my requirement is that I start the first loop after some records if the second loop record exists.

Can I increase the value of index inside the second loop or how do I approach it?

Thanks.

1 ACCEPTED SOLUTION

former_member1360394
Participant
0 Kudos

I have resolved the problem myself.

Thank you so much guys for helping it out.

21 REPLIES 21

former_member203305
Active Contributor
0 Kudos

Hi.

create a variable l_index and use it in the second loop.

there u can increase the value of the l_index as you want.

second loop.

l_index = sy-tabix.

regards

Miguel

0 Kudos

Hello Miguel,

I created one variable like index and increased the value in the second loop.

But when its going to the first loop, the value of index shows as 8, but its starting from 5.

Initially it had started from 4.

Regards,

Vinod

0 Kudos

Hi Vinod,

            Please place your coding ....

0 Kudos

Hi,

what i always do is to use local variable for index inside the loop. So, for the first loop also use a l_index1, for the second loop l_index2 and use them as you want.

But becareful, do not use l_index = sy-tabix, because it will be the same.

need to create ur own counter, means

l_index1 = 1 + l_index1.

then inside the second loop.

l_index2 = 1 + l_index2.

that's all

Regards

Miguel

0 Kudos

Miguel,

Thanks for the reply.

Please see the code here:

Read table itab into wa with key.

if sy-subrc = 0.

     index1 = sy-tabix.

     loop at itab into wa1 from lw_index1.

          read table itab1 into wa2 with key.

          if sy-subrc = 0.

               index = sy-tabix.

               loop at itab1 into wa3 from index.

                   ***** my code here****

                    index1 = index1 + 1.

               endloop.

          endif.

     endloop.

endif.

0 Kudos

I meant

loop at itab into wa1 from index1.

0 Kudos

hi.

it should work...just check the result that u expect to get.

Regards

Miguel

0 Kudos

Miguel,

I just checked its not working.

Regards,

Vinod

0 Kudos

I think that no need of the first index in the first loop. Just need the index from the second loop...

   

     loop at itab into wa1.

          l_index1 = l_index1 + 1.

          read table itab1 into wa2 with key.

          if sy-subrc = 0.

               loop at itab1 into wa3 from index1.

                   ***** my code here****

               endloop.

          endif.

     endloop.

if not...please explain the original request to understand it better.

Reards

Miguel

0 Kudos

Miguel,

Thank you for the reply.

Basically the above code doesn't work for me.
I do the loop on the same table twice based on different conditions.

loop at itab into wa1 from index1.

          read table itab into wa2 with key.

          if sy-subrc = 0.

               index = sy-tabix.

               loop at itab into wa3 from index.

                   ***** my code here****

                    index1 = index1 + 1.

               endloop.

          endif.

     endloop.

0 Kudos

ok..rare.

well, check this condition.

loop at itab into wa1 from index1.

          read table itab into wa2 with key.

          if sy-subrc = 0.

               index = sy-tabix.

replace for

loop at itab into wa1 from index1.

         index = index + 1.

          read table itab into wa2 with key.

          if sy-subrc = 0.

             

because sy-tabix always changes ...better to use ur own counter as i told you.

Regards

Miguel

0 Kudos

Miguel,

Can you kindly explain more as its confusing. 🙂

Regards,

Vinod

0 Kudos

you are using 2 loops, the variables are the same sy-tabix for both loops.

So rather than use sy-tabix, use a local variable that does the same.

l_index = l_index + 1.

replace the code that i sent u and check it.

Regards

Miguel

0 Kudos

Miguel,

Sorry to ask but can you give me the code as I understood the explanation but not able to get how to write it.

Regards,

Vinod

0 Kudos

loop at itab into wa1 from index1.

         index = index + 1.

          read table itab into wa2 with key.

          if sy-subrc = 0.

              loop at itab1 into wa3 from index.

                   ***** my code here****

                    index1 = index1 + 1.

               endloop.

          endif.

     endloop.

that's all

good luck...check the index...just index problem...

Regards

0 Kudos

Miguel,

Its not working.

Index1 value is 8 when it comes out of second loop.

But when it goes to the first loop, it is starting from 5 as initially it had started from 4.

Regards,

Vinod

0 Kudos

mmm...so why do you need the index1 in the first loop. that one should always be looping without index as i understood. sorry i dont get what u need...

Regards

Miguel

thangam_perumal
Contributor
0 Kudos

Hi Vinod,

               Please refer below coding i think it will satisfies your requirements.

DATA: I TYPE I.

I = 1.

LOOP AT ITAB1 INTO WA1.

  do.

    READ TABLE ITAB2 INTO WA2 INDEX I.

    IF SY-SUBRC <> 0. EXIT. ENDIF.

    IF WA2-K < WA1-K.

      ADD 1 TO I.

    ELSEIF WA2-K = WA1-K.

      " ...

      ADD 1 TO I.

      EXIT.

    ELSE.

      EXIT.

    endif.

  enddo.

  if sy-subrc <> 0. exit. endif.

ENDLOOP.

Regards,

    Thangam.P

former_member1360394
Participant
0 Kudos

I have resolved the problem myself.

Thank you so much guys for helping it out.

0 Kudos

Hi Vinod,

             we are happy to hear the problem has been solved by your self...

Regards,

  Thangam.P

0 Kudos

Since I was looping the same table twice.

I added the index inside the second loop.

Below shows the result:

Loop at itab1 into wa1.

     read table itab1 into wa2 with key.

     index1=sy-tabix.

          loop at itab1 into wa3 from index1.

               index 1 = index1 + 1.

               *my code*

          endloop.

endloop.