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: 

FOR loop in ABAP Code ?

praveenkumar_kadi
Active Contributor
0 Kudos

Hi,

I am working on a ABAP requirement, where I need to use a dynamic variable attached to a compilable code.

For ex:

In Jave we use

for( i=0; i >n; i++){

int i=0;

stmti+stmti = result;

}

Here i is an dynamic variable and it will be keep on changing ATTACHED TO stmti as stmt1.stmt2....etc. whenever there is a

change in i.

In ABAP, I have below code to attach:- Here I need to change from 1 to 100 like below. currently I see it as re-work, if i put the code each time changing manually. so I want make it in a loop and so dynamic and hence less code. How to achieve this. Please

help. Any ABAP string functions to be used?

if i=1

DATA ls_nd_tmp1 TYPE wd_this->Element_nd_tmp1.

DATA it_nd1 TYPE TABLE OF wd_this->Element_nd_tmp1.

DATA wa_nd1 TYPE wd_this->Element_nd_tmp1.

if i=2

DATA ls_nd_tmp2 TYPE wd_this->Element_nd_tmp2.

DATA it_nd2 TYPE TABLE OF wd_this->Element_nd_tmp2.

DATA wa_nd2 TYPE wd_this->Element_nd_tmp2.

Thanks

Praveen

Edited by: Praveen kumar Kadi on Sep 23, 2010 9:59 AM

6 REPLIES 6

Former Member
0 Kudos

HI,

Try using While Loop as :

WHILE( i< 100)

+ your logic+

increment i.

ENDWHILE.

0 Kudos

Hi,

Thanks, but I need attach to existing compilable code..as I said in the post.

These are compilable statements, How do we add that extra 1, 2, 3.... in a a loop same time it should be compiled.

Please assume below is the code need to be in loop...!!

Actual declaration without ABAP loop used.

DATA ls_nd_tmp1 TYPE wd_this->Element_nd_tmp1.

DATA it_nd1 TYPE TABLE OF wd_this->Element_nd_tmp1.

DATA wa_nd1 TYPE wd_this->Element_nd_tmp1.

if i=2

DATA ls_nd_tmp2 TYPE wd_this->Element_nd_tmp2.

DATA it_nd2 TYPE TABLE OF wd_this->Element_nd_tmp2.

DATA wa_nd2 TYPE wd_this->Element_nd_tmp2.

Actual declaration with ABAP loop used.(to attach 1,2,3...etc..dynamically. Just for example I added "+i" below)

DO

DATA ls_nd_tmpi TYPE wd_this->Element_nd_tmpi.

DATA it_ndi TYPE TABLE OF wd_this->Element_nd_tmpi.

DATA wa_ndi TYPE wd_this->Element_nd_tmpi.

i++

END.

Something similar to the above is possible ?

Thanks

Praveen

0 Kudos

Hi,

In ABAP you can use the Perform statement to achieve this task.

Perform <subroutinee name> using ls_nd_tmp1 type wd_this->Element_nd_tmp1

it_nd1TYPE TABLE OF wd_this->Element_nd_tmp1 wa_nd1

wa_nd1 TYPE wd_this->Element_nd_tmp1.

Perform <subroutinee name> using ls_nd_tmp2 type wd_this->Element_nd_tmp2

it_nd2 TYPE TABLE OF wd_this->Element_nd_tmp2

wa_nd2 TYPE wd_this->Element_nd_tmp2.

Perform <subroutinee name> using ls_nd_tmp100 type wd_this->Element_nd_tmp100

it_nd100 TYPE TABLE OF wd_this->Element_nd_tmp1

wa_nd100 TYPE wd_this->Element_nd_tmp100.

form <subroutinee name> using p_nd_tmp type wd_this->Element_nd_tmp

p_it_nd TYPE TABLE OF wd_this->Element_nd_tmp

p_wa_nd TYPE wd_this->Element_nd_tmp.

<logic>

endform.

regards,

Sakshi

Former Member
0 Kudos

Hi,

Give the actual requirement.

For ex, In COSP table we have 16 columns for cost. Requirement is to add the columns for each of the period separately. But, we don't want to give

sum_wlp01 = sum_wlp01+ wa_cosp-wlp01.

.

.

.

sum_wlp16 = sum_wlp16+ wa_cosp-wlp16.

In this case we use field symbols to make it simple. You can find threads resolving the same.

Hope it helps.

Sujay

0 Kudos

Hi Sujay,

Thanks. Thats' same here, How do you used field symbol to replace your table numbers dynamically? same it time it should be complled.

Please post your sample code to understand better.

Thanks

Praveen

matt
Active Contributor
0 Kudos

>

>

> if i=1

> DATA ls_nd_tmp1 TYPE wd_this->Element_nd_tmp1.

> DATA it_nd1 TYPE TABLE OF wd_this->Element_nd_tmp1.

> DATA wa_nd1 TYPE wd_this->Element_nd_tmp1.

>

> if i=2

> DATA ls_nd_tmp2 TYPE wd_this->Element_nd_tmp2.

> DATA it_nd2 TYPE TABLE OF wd_this->Element_nd_tmp2.

> DATA wa_nd2 TYPE wd_this->Element_nd_tmp2.

>

What are you going to do with this data once you've defined it? I'd define an internal table with three fields -

TYPES: BEGIN OF my_elements_ty,
        r_element1 TYPE REF TO data,
        r_element2 TYPE REF TO data,
        r_element3 TYPE REF TO data,
      END OF my_elements_ty.

DATA: lt_elements TYPE STANDARD TABLE OF my_elements_ty,
      ls_element  TYPE my_elements_ty.

Then, for each iteration, build the name of the element type in strings and use something like:

CREATE DATA ls_element-r_element1 TYPE (the_type_in_the_string1).    
CREATE DATA ls_element-r_element1 TYPE (the_type_in_the_string2).    
CREATE DATA ls_element-r_element1 TYPE (the_type_in_the_string3).

INSERT ls_element INTO TABLE lt_elements.

But this really is advanced ABAP, and not suitable for someone who is clearly a beginner. ( Also note that these forums are not supposed to be a substitute for training - or reading an ABAP tutorial ).

matt