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: 

how to store every alternative loop pass in different variable

Former Member
0 Kudos

Hi All,

I have an internal table in which i want to store every alternative pass in another variable. for example for the first pass i want to store in var1, second pass i want to store in var2, again the 3rd pass will go into var1 and 4 th pass will have to go in var2. I mean all the odd passes has to go in var1 and even passes has to go in var2.

is there any logic using loop at or some thing else.

Thanks in advance

6 REPLIES 6

Former Member
0 Kudos

Hi,

Here is the code.

DATA:

W_REMAINDER LIKE SY-TABIX.

LOOP AT ITAB.

W_REMAINDER = SY-TABIX MOD 3.

CASE W_REMAINDER.

WHEN 1.

W_TEMP = W_VAR1.

WHEN 2.

W_TEMP = W_VAR2.

WHEN 3.

W_TEMP = W_VAR3.

ENDCASE.

ENDLOOP.

Regards,

Rama Chary.Pammi

matt
Active Contributor
0 Kudos

He said every alternate one - not every third one.


data: l_flipflop type i.

l_flipflop = 1.

loop at my_tab into my_wa.
  IF l_flipflop EQ '1'.
    cumulate into Var1.
  ELSE.
    cumulate into Var2.
  ENDIF.
  l_flipflop = 0 - l_flipflop.
endloop 

Former Member
0 Kudos

Hello,

You might try something like.

loop at it_mtab into is_mytab.

if sy-tabix mod 2 = 0 " ( sy-tabix is even)

var1 = is_mytab-var.

else.

var2 = is_mytab-var.

endif.

endloop.

Regards

Greg Kern

matt
Active Contributor
0 Kudos

Neater than my solution - for standard tables. But it won't work for non-indexed tables like HASHED tables, cos they don't update sy-tabix.

Former Member
0 Kudos

Try this way...

loop at itab1.

g_temp = sy-index mod 2. (check syntax)

if g_temp = 0.

move data to val1 and append.

else.

move data to val2 and append.

endif.

endloop.

naimesh_patel
Active Contributor
0 Kudos

Try something like this:



TYPES: BEGIN OF TY_DATA,
       VBELN  TYPE VBELN,
       FIELD  TYPE CHAR10,
       END   OF TY_DATA.

TYPES: BEGIN OF TY_TAB,
       VBELN TYPE VBELN,
       FIELD1 TYPE CHAR10,
       FIELD2 TYPE CHAR10,
       END   OF TY_TAB.

DATA: T_DATA TYPE STANDARD TABLE OF TY_DATA,
      T_TAB  TYPE STANDARD TABLE OF TY_TAB,
      WA_DATA TYPE TY_DATA,
      WA_TAB  TYPE TY_TAB.

DO 6 TIMES.
  WA_DATA-VBELN  = SY-ABCDE+0(5).
  WA_DATA-FIELD  = SY-INDEX.
  APPEND WA_DATA TO T_DATA.
ENDDO      .

DATA: L_CNT TYPE I,
      L_DONE TYPE C.

LOOP AT T_DATA INTO WA_DATA.
  L_CNT = L_CNT + 1.
  CLEAR L_DONE.
  CASE L_CNT.
    WHEN 1.
      WA_TAB-VBELN = WA_DATA-VBELN.
      WA_TAB-FIELD1 = WA_DATA-FIELD.
    WHEN 2.
      WA_TAB-FIELD2 = WA_DATA-FIELD.
      APPEND WA_TAB TO T_TAB.
      CLEAR L_CNT.
      L_DONE = 'X'.
  ENDCASE.

  AT LAST.
    IF L_DONE IS INITIAL.
      APPEND WA_TAB TO T_TAB.
    ENDIF.
  ENDAT.

ENDLOOP.

Regards,

Naimesh Patel