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: 

eliminating loops

Former Member
0 Kudos

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

5 REPLIES 5

naveen_inuganti2
Active Contributor
0 Kudos

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

Former Member
0 Kudos

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.

former_member194613
Active Contributor
0 Kudos

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:

Siegfried

former_member585060
Active Contributor
0 Kudos

Hi,

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

Regards

Krishna

Former Member
0 Kudos

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