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

Dynamic loops

Former Member
0 Likes
1,460

Hi friends,

I have got a wierd sort of requirement.Suppose my select query fetches 3 rows which contains values CT1,CT2,CT3.then my loop should go this way.

do CT1 times.

-


-


do CT2 times.

-


-


do CT3 times.

-


-


enddo.

enddo.

enddo.

But if select query fetches 5 rows then it should be..

do CT1 times.

-


do CT2 times.

-


do CT3 times.

-


do CT4 times.

-


do CT5 times.

-


enddo.

enddo

enddo.

enddo.

enddo.

This should be dynamic in my ABAP code.How can i fulfill this requirement.

Thanks and Regards,

Gaurav

9 REPLIES 9
Read only

Former Member
0 Likes
1,182

Hi,

Please mention the actual requirement..and probably we can have different approach..

Thanks

Naren

Read only

Former Member
0 Likes
1,182

HI,

You will need an outer loop to loop at the master table from which you are fetching the vaules for CT1.. CT2 and so on.



loop at itab into wa

do wa-(counter_field) times.
enddo.

endloop.

Now the inner loop will run as many records are there in the itab.

regards,

Advait

Read only

ThomasZloch
Active Contributor
0 Likes
1,182

I can only think of dynamic program generation, see GENERATE SUBROUTINE POOL.

http://help.sap.com/saphelp_470/helpdata/en/9f/db999535c111d1829f0000e829fbfe/frameset.htm

Thomas

Read only

Former Member
0 Likes
1,182

Instead of nested DOs, why not just take the factorial of the number you retrieve and have a single DO using that factorial.

And while you're at it, consider cleaning up your unresolved posts:

Total Questions: 110 (95 unresolved)

Rob

Read only

0 Likes
1,182

Hi Rob,

I cannot do so..if i am getting say CT1 = 10,CT2 =5,CT =6,this does not mean that i am going to take 10,5 and 6 as my values. the values may vary runtime but my only concern is to get the dynamic do loops.It could be CT1,CT2...CT5 and it may be CT1,CT2.....CT10..

Thanks and Regards,

Gaurav

Read only

0 Likes
1,182

Hi Gaurav,

Can you let me know if the statements to be processed are same in all the do loop which are created dynamically.

if its the same.

the you can add up the all the values of field and then run the do loop at once.

example.

if ct1 = 5.

ct2 = 6,

ct3 = 10.

then you take a temp variable and then add them

temp = ct1 + ct2 + ct3.

do temp times.

<tatement>

enddo.

Hope this helps you.

Regards,

Siddarth

Read only

Former Member
0 Likes
1,182

Hi Gaurav,

select 'col'

from ' '

into i_tab.

describe table i_tab lines w_lines. '' u gt no. of lines in i_tab

w_idx = 1.

do w_lines times.

read table i_tab index w_idx into work_area.

do work_area-col times.

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

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

enddo.

add 1 to w_idx.

enddo.

Hope this gives u an idea to proceed..

Regards,

Mdi.Deeba

Read only

Former Member
0 Likes
1,182

Hi,

I am using SFLIGHT Table for example.Suppose i have to process according to the no of floghts avilable(This is like the no of rows retrived.) The no of seats occupied is like the CT1,CT2.., According to your requirement. So i have to run the loops those many times.

Suppose i am retr

data:

t_itab type table of sflight with header line.

select * from sflight into table t_itab.

loop at t_itab.

do t_itab-seatsocc_f times. " seatsocc_f is no of seats occupied in particular flight.

" code.

enddo.

endloop.

Thanks,

Nithya

Read only

Former Member
0 Likes
1,182

Hi Gaurav,

Hi Gaurav,

Follow the stpes and build code as pe that.

1.first you get the records fom your table to internal table .so your internl table will have the records.

2.Now to get how mant records you have in your internal table you can write as

describe table i_tab lines no_lines. '' u get teh no. of lines in i_tab

3,.now to know the values of internal table suppose your internal table contains 3 records ct ct2 ct3.

Now you can loop the values from internal table to work area and then do that many times one by one.

then you will get your dynamic things .

please ttry in this way ,

Thanks

Rajendra