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..

naveen_inuganti2
Active Contributor
0 Likes
1,721

Hi...Friends,

>loop at itab1.

>

>/////

>

> loop at itab2 where f1 = itab1-f1.

>

>///////

>

> endloop.

>

>endloop.

Here Iam having more than one entreis in table itab2 for each itab-f1 entry.

I want the same functionality with out using where condition for second loop.

( Get back to you... If you want to know my orginal problem..at closing of this thread... )

Thanks,

Naveen.I

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,690

Hi,

You better use

Read table statement and get the no of lines of the internal table (Say it is N).

and DO N times ..ENDDO loop

Regards,

Anirban

17 REPLIES 17
Read only

Former Member
0 Likes
1,691

Hi,

You better use

Read table statement and get the no of lines of the internal table (Say it is N).

and DO N times ..ENDDO loop

Regards,

Anirban

Read only

0 Likes
1,690

Hi Experts....

here read statement with "with key statement" is not working.........

Any other replies...

(Hi freinds... I deffenately get back with my problem in this thread...give me some time.. plz try logic.. )

Thanks,

Naveen.I

Edited by: Naveen Inuganti on Jul 16, 2008 12:44 PM

Read only

0 Likes
1,690

hi,

you can use

DESCRIBE <ITAB>

statement.

CHECK THE SYNTAX

DESCRIBE TABLE <itab> [LINES <l>] 
                                       [OCCURS <n>] 
                                       [KIND <k>].

Here LINES will give you total line no.

REGARDS,

ANIRBAN

Read only

0 Likes
1,690

Hi Naveen.

Use parallel cursor technique which is much faster than loop at where clause.


SORT itab1 BY f1,
          itab2 BY f1.

  MOVE 1 TO w_index.
  LOOP AT itab1 INTO wa1.
    LOOP AT itab2 INTO wa2 FROM w_index.
      IFwa1-f1 NE wa-f2.
        w_index = sy-tabix.
        EXIT.
      ENDIF.
Do your processing
    ENDLOOP.
  ENDLOOP.

Check below sample code for usage of parallel cursor tech.(Comparision also.)


TABLES: vbak.

SELECT-OPTIONS: so_vbeln FOR vbak-vbeln,
                so_erdat FOR vbak-erdat.
PARAMETERS: po_rows TYPE i.
TYPES: BEGIN OF t_vbak,
         vbeln TYPE vbak-vbeln,
         erdat TYPE vbak-erdat,
       END OF t_vbak,

       BEGIN OF t_vbap,
         vbeln TYPE vbap-vbeln,
         posnr TYPE vbap-posnr,
       END OF t_vbap.

DATA: i_vbak  TYPE STANDARD TABLE OF t_vbak,
      i_vbap  TYPE STANDARD TABLE OF t_vbap,
      wa_vbak TYPE t_vbak,
      wa_vbap TYPE t_vbap,
      w_time1 TYPE i,
      w_time2 TYPE i,
      w_time3 TYPE i,
      w_count TYPE i,
      w_index TYPE sy-index.

SELECT vbeln erdat UP TO po_rows ROWS
       INTO TABLE i_vbak
       FROM vbak
       WHERE vbeln IN so_vbeln
       AND erdat IN so_erdat.

WRITE:/1 'Total number of header records', sy-dbcnt.

CHECK NOT i_vbak[] IS INITIAL.
SELECT vbeln posnr
       INTO TABLE i_vbap
       FROM vbap
       FOR ALL ENTRIES IN i_vbak
       WHERE vbeln EQ i_vbak-vbeln.

WRITE:/1 'Total number of item records', sy-dbcnt.
SORT: i_vbak BY vbeln,
      i_vbap BY vbeln.

DO 5 TIMES.
  CLEAR: w_time1, w_time2, w_time3, w_count.
  GET RUN TIME FIELD w_time1.
*Loop at where clause
  LOOP AT i_vbak INTO wa_vbak.
    LOOP AT i_vbap INTO wa_vbap WHERE vbeln EQ wa_vbak-vbeln.

    ENDLOOP.
  ENDLOOP.

  GET RUN TIME FIELD w_time2.
  w_time3 = w_time2 - w_time1.
  WRITE: /1 'Time taken for where clause', w_time3,
         /1 'Number matching records', w_count.

  CLEAR: w_time1, w_time2, w_time3, w_count.
  GET RUN TIME FIELD w_time1.
  *Parallel cursor technique
  MOVE 1 TO w_index.
  LOOP AT i_vbak INTO wa_vbak.
    LOOP AT i_vbap INTO wa_vbap FROM w_index.
      IF wa_vbak-vbeln NE wa_vbap-vbeln.
        w_index = sy-tabix.
        EXIT.
      ENDIF.
    ENDLOOP.
  ENDLOOP.

  GET RUN TIME FIELD w_time2.
  w_time3 = w_time2 - w_time1.
  WRITE: /1 'Time taken for parallel processing' , w_time3,
         /1 'Number matching records', w_count.
ENDDO.

Thanks,

Vinod.

Read only

Former Member
0 Likes
1,690

HI,

You can store sy-tabix in a variable and then loop at itab2 from <variable>.

This is more eficient in such cases.

Read only

Former Member
0 Likes
1,690

hi,

Can You elaborate the problem.

Regards

Sumit Agarwal

Read only

KalC
Active Participant
0 Likes
1,690

HI ,

You can remove where condition , use from .. to and put if condition inside that second loop.if successfull capture sy-tabix into a variable use this variable in loop from..to.Hope it helps.

Regards,

Kalyan.

Read only

Former Member
0 Likes
1,690

hi,

i think we can use the concept of parallel cursors i.e., using sy-tabix we can ctrl the loop iteration.

Rgds.,

subash

Read only

Former Member
0 Likes
1,690

Hi,

Loop at itab1.

Loop at itab2.

if itab1-f1 = itab2-f1.

write: 'Match found'.(What you want ot Display)

endif.

Endloop.

Endloop.

Regards,

Sujit

Read only

Former Member
0 Likes
1,690

Hi Naveen,

Solution:

sort itab2 by f1.

loop at itab1.

..............

read table itab2 with key f1 = itab1-f1 binary search.

if sy-subrc = 0.

w_index = sy-tabix.

loop at itab2 from w_index.

if itab2-f1 = itab1-f1.

..........

else.

exit.

endif.

endloop.

...............

endloop.

if it useful, reward points.

Thank you,

Prasad G.V.K

Read only

Former Member
0 Likes
1,690

hiiii

here you can use READ statement but before that what ever field you are using in where condition ..sort second table by that field.then instead of using second loop condition use READ statement & in that use IF condition...then write whatever you want to display...i think it will work..

loop at itab1..

sort itab2 by ....

read table i tab2...

if condition...

display()

endif.

endloop.

regards

twinkal

Read only

Subhankar
Active Contributor
0 Likes
1,690

Hi,

Try to avoid loop inside a loop.

Instead of loop inside a loop use parallel cursor to enhance the performance.

Please try in this way.

loop at i_tab1 into wa_work1.

read table I-tab2 transporting no field with key f1 = wa_work1-f1.

if sy-subrc = 0.

l_tabix = sy-tabix.

loop at i_tab2 into wa_work2 from l_tabix.

if wa_work2-f1 = wa_work1-f1.

-


do ur work

else

exit.

endloop.

endif.

endloop.

Read only

Former Member
0 Likes
1,690

Hai Naveen,

loop at itab1.

/////

read table itab2 with key f1 = itab1-f1.

if sy-subrc eq 0.

///////

endif.

endloop.

Regards,

Harish

Read only

naveen_inuganti2
Active Contributor
0 Likes
1,690

HI..... Friends....

That was really very great responce from ur side....

Actually where condition is not working for loop..,

And also read statement also not working with "with key" statement...for the internal table in changing parameter of subroutine which is in the include program of function module..

there is one more thread with folloing subject.. with complete code...

Subroutine showing error in include program with changing parameter

Here so many solutions are there...I will try..

Get back to you with result....

Thanks a lot SDN friends,

Naveen Inuganti

Edited by: Naveen Inuganti on Jul 16, 2008 1:12 PM

Read only

Former Member
0 Likes
1,690

Hi,

Why don't you try doing like

loop at itab1.

*steps to be done for itab1 data

/////

endloop.

above loop for processing of itab1 data and

for common data do it as below:

loop at itab2.

read itab1 where f1 = itab2-f1.

*steps for common data in itab1 and itab2

///////

endloop.

As I suppose there is a unique f1 in itab1 for multiple f1 in itab2.

I hope this solves the problem.

Read only

0 Likes
1,690

Hi.... Hitish...,

No there is number of f1s in itab1...

and..,

Now may i know what is the entry for itab2-f1 in read statement.. in ur code...

Thnaks for ur reply,

Naveen.I

Read only

0 Likes
1,690

Hi Naveen,

The itab2-f1 in the prev code is the f1 in itab2 for which we find data in itab1 (which i thought was a single record).

If there are a number of f1s in itab1 and itab2 both u can try this

sort itab2 by f1.

loop at itab1.

*steps for itab1 data

/////

read itab2 with key f1 = itab1-f1 binary search transporting no fields.

*this will give index of first f1 in table itab2

lv_index = sy-tabix

loop at itab2 from lv_index.

if itab2-f1 ne itab1-f1.

exit.

endif.

*steps for common data in itab1 and itab2

///////

endloop.

*above loop will run same as loop with where condition

endloop.

Hope this will help.

Reward points if it works for u.

Thanks,

Hitesh