Skip to Content
author's profile photo Former Member
Former Member

eliminating loops

Hi experts

I hav 3 loops in a single loop

i selected the data in an internal table and read it in the loops

with 3 tables

i need to eliminate nested loop.

how to do it

Add a comment
10|10000 characters needed characters exceeded

Related questions

5 Answers

  • Posted on Aug 12, 2008 at 12:42 PM

    Hi....

    You can write READ statement inside the loop.

    Check this code...

    Declaration part.... with two internal atbels and one output internal table...

    >types: begin of ty_model1,

    > za(10),

    > zb type netwr,

    > zc(10),

    > zd(10),

    > ze(10),

    > zf(10),

    > end of ty_model1,

    > begin of ty_model2,

    > za1(10),

    > zb1(10),

    > zc1(10),

    > zd1(10),

    > za(10),

    > end of ty_model2,

    > begin of ty_output,

    > za(10),

    > zb type netwr,

    > zc(10),

    > zd(10),

    > ze(10),

    > zf(10),

    > za1(10),

    > zb1(10),

    > zc1(10),

    > zd1(10),

    > end of ty_output.

    >

    >data: t_model1 type standard table of ty_model1 initial size 0,

    > t_model2 type standard table of ty_model2 initial size 0,

    > t_output type standard table of ty_output initial size 0,

    > w_model1 type ty_model1,

    > w_model2 type ty_model2,

    > w_output type ty_output.

    In the start-of-selection...

    >loop at t_model2 into w_model2.

    > w_output-za1 = w_model2-za1.

    > w_output-zb1 = w_model2-zb1.

    > w_output-zc1 = w_model2-zc1.

    > w_output-zd1 = w_model2-zd1.

    >

    >read table t_model1 into w_model1 with key za = w_model2-za. <--like this you can write another read statements

    >if sy-subrc = 0.

    > w_output-za = w_model1-za.

    > w_output-zb = w_model1-zb.

    > w_output-zc = w_model1-zc.

    > w_output-zd = w_model1-zd.

    > w_output-ze = w_model1-ze.

    > w_output-zf = w_model1-zf.

    >endif.

    >append w_output to t_output.

    >clear: w_output, w_model1, w_model2.

    >endloop.

    Here zmodel1, zmodel2 are database table...

    Thanks,

    Naveen.I

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Aug 12, 2008 at 12:46 PM

    Hi,

    Try in this manner.

    suppose if there are 3 tables t1,t2,t3.

    Loop at t1 into fs1.

    Read table t2.

    read table t3.

    move all the contents to another internal table.

    append t4.

    endloop.

    Regards.

    Eshwar.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Aug 12, 2008 at 01:14 PM

    A Read is not a loop and a read can not replace a loop!

    That comes from the recommendation 'Avoid nested Loops' ....

    but either you need nested loops or you don't need nested loops.

    If you need then, then there is no way to avoid them. However, there are lots of solution which cause no performance problems.

    The recommended solution is to use a sorted table for the table inside the loop!

    loop at itab1 in wa1.
      loop at itab2 into wa2
             where f1 = wa1-f1 ...
       ....
      endloop.
    endloop.
    

    If you would use a standard table then the performance is poor, with sorted tables it is perfectly o.k.

    You will need a loop if several lines can fulfilled the condition (1:c relation), and you can use a read if there is a 1:1 relation.

    But again, the read on a sorted table is fine, the read on a standard table not, if not bionary search is used.

    There is way to use binary search also for the loop, which can be found here:

    Measurements on internal tables: Reads and Loops:

    Runtimes of Reads and Loops on Internal Tables

    Siegfried

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Aug 12, 2008 at 05:22 PM

    Hi,

    Regarding nested loop the blog of Rob Burbank Performance of Nested Loops is a "Must Read".

    Regards

    Krishna

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Aug 13, 2008 at 08:58 AM

    Hi,

    Take out the innermost two loops.

    And use read table itab index statement as many times as you want.

    But each time make sure that you r passing the currect index.

    Regards,

    Rama chary.P

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.