Skip to Content

FOR loop in ABAP Code ?

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • avatar image
    Former Member
    Sep 23, 2010 at 08:14 AM

    HI,

    Try using While Loop as :

    WHILE( i< 100)

    + your logic+

    increment i.

    ENDWHILE.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Praveen kumar Kadi

      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

  • avatar image
    Former Member
    Sep 23, 2010 at 08:46 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

    • 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

  • Sep 23, 2010 at 09:01 AM

    >

    >

    > 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

    Add comment
    10|10000 characters needed characters exceeded