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: 

Increment variable dynamically in LOOP

Former Member
0 Kudos
2,709

Hi All,

I have a situation where I have to loop at a table and fill an internal table while incrementing the variable name of the internal table at the same time.

For instance I have three variables: var1, var2 and var3. In the first loop I want to assign the value to Var1, in the 2nd loop to var2 and in the third loop to var3 and so on.

types: Begin of t_second

var1 TYPE C,

var2 TYPE C,

var3 TYPE C,

end of t_second.

data : gt_second type table of t_second,

wa_second type t_second.

LOOP at gt_first into wa_first.

MOVE: wa_first-var TO wa_second-var1, <---- in the 2nd loop I want to make this var1 to var2 and so on.

APPEND wa_first TO gt_second

CLEAR wa_first

ENDLOOP.

Will it be possible to achieve this with ABAP?

Thanks.

Edited by: mark mark on Mar 1, 2009 8:49 AM

7 REPLIES 7

Former Member
0 Kudos
347

USE:

PERFORM: sy-tabix OF sub1 sub2 sub3.

as the value of sy-tabix increases value subroutine will be executed.

Ex:

When SY-tabix = 1--->SUB1

When 2----->SUB2

TYPES: BEGIN OF t_second,
var1 TYPE c,
var2 TYPE c,
var3 TYPE c,
END OF t_second.

DATA : gt_second TYPE TABLE OF t_second,
wa_second TYPE t_second,
wa_first(20) TYPE c,
gt_first TYPE TABLE OF t_second.

wa_first = 'TEST'.
append wa_first to gt_first.

wa_first = 'TEST'.
append wa_first to gt_first.

wa_first = 'TEST'.
append wa_first to gt_first.
LOOP AT gt_first INTO wa_first.

  PERFORM: sy-tabix OF sub1 sub2 sub3.

*   MOVE: wa_first TO wa_second-var1.  " <---- in the 2nd loop i want to make this var1 to var2 and so on.

  APPEND wa_first TO gt_second.
  CLEAR wa_first.
ENDLOOP.

*&---------------------------------------------------------------------*
*&      Form  sub1
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM sub1.

  MOVE: wa_first TO wa_second-var1.
ENDFORM.                                                    "sub1

*&---------------------------------------------------------------------*
*&      Form  sub2
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM sub2.
  MOVE: wa_first TO wa_second-var2.
ENDFORM.                                                    "sub2

*&---------------------------------------------------------------------*
*&      Form  sub3
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM sub3.
  MOVE: wa_first TO wa_second-var3.
ENDFORM.                                                    "sub3

Regards,

Gurpreet

Private_Member_14913
Contributor
0 Kudos
347

Hello,

you can use case endcase statement.

LOOP at gt_first into wa_first.

CASE sy-tabix.

when 1.

MOVE: wa_first-var TO wa_second-var1.

when 2.

MOVE: wa_first-var TO wa_second-var2.

when 3.

MOVE: wa_first-var TO wa_second-var3.

when others.

  • message for no variable to transfer data

endcase.

APPEND wa_first TO gt_second.

CLEAR wa_first.

endloop.

Former Member
0 Kudos
347

use field symbols

Former Member
0 Kudos
347

Hi,

I understood your Requirement. I am giving you one dynamic Solution. This is applicable in all cases. Like Uploading data from Excel, or Building Dynamic fieldcats.

Illustration:

data: Begin of wa1,

var type char10, " (take any length you want)

end of wa1,

itab1 like standard table of wa1.

data: begin of wa2,

var1 type char6,

var2 type char7,

var3 type char8,

var4 type char9,

var5 type char10,

end of wa2,

itab2 like standard table of wa2.

field-symbols: <FS1> type any.

parameters: p_a RADIOBUTTON GROUP gr1 default 'X', " fill in diagonal

p_b RADIOBUTTON GROUP gr1. " fill in left triangle

start-of-selection.

wa1-var = 'Balaji'. Append wa1 to itab1.

wa1-var = 'Balaji1'. Append wa1 to itab1.

wa1-var = 'Balaji12'. Append wa1 to itab1.

wa1-var = 'Balaji123'. Append wa1 to itab1.

wa1-var = 'Balaji1234'. Append wa1 to itab1.

loop at itab1 into wa1.

assign component sy-tabix of structure wa2 to <FS1>.

if sy-subrc = 0.

<fs1> = wa1-var.

append wa2 to itab2.

unassign <fs1>.

if p_b is INITIAL.

clear: wa2.

endif.

else.

exit.

endif.

endloop.

Try this one.

Thank you,

Balaji Peethani.

former_member218674
Contributor
0 Kudos
347

Hello,

This is the sample code:

data variable type char5.

data no type char2.

data vartab type table of char5 with header line.

do 5 times.

pack sy-tabix to no.

concatenate 'Var' no into variable.

append variable to vartab.

enddo.

loop at vartab.

write: / vartab.

endloop.

Hope this helps you.

Thank,

Augustin.

0 Kudos
347

I hope you adopt one of the last three solutions. The others are nice efforts, but not right.

matt

Former Member
0 Kudos
347

Hi mark,

kindly chk the followingg code , it works according to the requirement.


types: Begin of t_second,
var1 TYPE C,
var2 TYPE C,
var3 TYPE C,
end of t_second.

data : gt_second type table of t_second,
wa_second type t_second.

types: begin of t_first,
  var type c,
  end of t_first.

  data: gt_first type table of t_first,
        wa_first type t_first.

data: w_idx type i value 1.

do 9 times.
  wa_first-var = sy-index.
  append wa_first to gt_first.
  enddo.
LOOP at gt_first into wa_first.
  case w_idx.
    when 1.
      CLEAR wa_second.
      MOVE: wa_first-var TO wa_second-var1.
      APPEND wa_second TO gt_second.
      w_idx = 2.
     when 2.
       CLEAR wa_second.
       MOVE: wa_first-var TO wa_second-var2.
       APPEND wa_second TO gt_second.
       w_idx = 3.
      when 3.
        CLEAR wa_second.
        MOVE: wa_first-var TO wa_second-var3.
        APPEND wa_second TO gt_second.
        w_idx = 1.
  ENDCASE    .
ENDLOOP.

loop at gt_second into wa_second.
  write:/ wa_second-var1,
           wa_second-var2,
            wa_second-var3.
endloop.

Regards