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

Internal table logic

Former Member
0 Likes
1,306

Hi Folks,

I am having an table with data like

X  Y  Z

10 20 30

50 60 70

I wanted to create an internal table as

1 X 10

1 Y 20

1 Z 30

2 X 50

2 Y 60

2 Z 70

Could you please give me some input on this.

Help is appreciated.

Thanks,

Smriti

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,277

Try below:


l_v_line = 0.

Loop at table_1 into wa_1.

l_v_line = l_v_line+1


wa_2-fld1 = l_v_line.

wa_2-fld2 = 'X'. "(Field_name1)
wa_2-fld3 = wa_1-fld1.
append wa_2 to table_2.

wa_2-fld2 = 'y'. "(Field_name2)
wa_2-fld3 = wa_1-fld2.
append wa_2 to table_2.

wa_2-fld2 = 'Z'. "(Field_name3)
wa_2-fld3 = wa_1-fld3.
append wa_2 to table_2.

clear wa_2.
Endloop.

7 REPLIES 7
Read only

Former Member
0 Likes
1,277

Create a dynamic table to build a table based on the input data.

Check this document

Will there be always only these three fields for the new table? And you just need to populate the appropriate values to these three fields? Then logic would be different. You would not need dynamic table.

Read only

0 Likes
1,277

yes always my table will have only these three fields only values will change.but internal table has to be the above  format

thanks for ur instant reply.please provide your input

Read only

0 Likes
1,277

Check this code

types : begin of ty1 ,
    x(2) ,
   y(2),
   z(2),
   end of ty1.

types : begin of ty2 ,
    no type i ,
    key ,
    value(2),
   end of ty2.

   data : itab1 type table of ty1, wa_itab1 type ty1,
           itab2 type table of ty2, wa_itab2 type ty2.

    wa_itab1-x = '10'.
   wa_itab1-y = '20'.
   wa_itab1-z = '30'.
   append wa_itab1 to itab1.

   wa_itab1-x = '40'.
   wa_itab1-y = '10'.
   wa_itab1-z = '20'.
   append wa_itab1 to itab1.

   wa_itab1-x = '60'.
   wa_itab1-y = '40'.
   wa_itab1-z = '20'.
   append wa_itab1 to itab1.

   data : lv_index type sy-tabix.
   FIELD-SYMBOLS <fs1>.


   loop at itab1 into wa_itab1.
     lv_index = sy-tabix.
     wa_itab2-no = lv_index.
     wa_itab2-key = 'X'.
     assign component 'X' of STRUCTURE wa_itab1 to <fs1>.
     if <fs1> is ASSIGNED.
       wa_itab2-value = <fs1>.
     endif.
     append wa_itab2 to itab2.
     wa_itab2-key = 'Y'.
     assign component 'Y' of STRUCTURE wa_itab1 to <fs1>.
     if <fs1> is ASSIGNED.
       wa_itab2-value = <fs1>.
     endif.
     append wa_itab2 to itab2.
      wa_itab2-key = 'Z'.
     assign component 'Z' of STRUCTURE wa_itab1 to <fs1>.
     if <fs1> is ASSIGNED.
       wa_itab2-value = <fs1>.
     endif.
     append wa_itab2 to itab2.
   endloop.

  

   loop at itab1 into wa_itab1.
        write 😕 wa_itab1-x, 4 wa_itab1-y, 7 wa_itab1-z.
    endloop.

   loop at itab2 into wa_itab2.
        write 😕 wa_itab2-no,  wa_itab2-key,  wa_itab2-value.
    endloop.



Read only

Former Member
0 Likes
1,277

Hi Smriti,

First create 2 internal table with same structure as shown by you.

move first index to 1 internal table and delete the first index from your old internal table.

then internal table shows like below.

new table

a b c

--------

X Y Z


old table

a   b  c

------------

10 20 30
50 60 70

then loop the old table and move the record to third internal table.

loop  at old.

read table new

a = sy-tabix.

b = new-a

c = old-a

append to third

a = sy-tabix.

b = new-b

c = old-b

append to third

a = sy-tabix.

b = new-c

c = old-c

append to third

endloop.


This will solve your problem.

regards,

sumit

Read only

philipdavy
Contributor
0 Likes
1,277

Hi Smriti,

Please find the code below.

TYPES: BEGIN OF TY_ITAB,

        X(2) TYPE C,

        Y(2) TYPE C,

        Z(2) TYPE C,

   END OF TY_ITAB.

DATA: ITAB TYPE STANDARD TABLE OF TY_ITAB,

       WA_ITAB LIKE LINE OF ITAB,

       IT_FINAL TYPE STANDARD TABLE OF TY_ITAB,

       WA_FINAL LIKE LINE OF IT_FINAL.

WA_ITAB-X = '10'.

WA_ITAB-Y = '20'.

WA_ITAB-Z = '30'.

APPEND WA_ITAB TO ITAB.

CLEAR: WA_ITAB.

WA_ITAB-X = '50'.

WA_ITAB-Y = '60'.

WA_ITAB-Z = '70'.

APPEND WA_ITAB TO ITAB.

CLEAR: WA_ITAB.

LOOP AT ITAB INTO WA_ITAB.

   WRITE: / WA_ITAB-X, WA_ITAB-Y, WA_ITAB-Z.

ENDLOOP.

DATA: COUNT TYPE I VALUE '0'.

LOOP AT ITAB INTO WA_ITAB.

   ADD  1 TO COUNT.

   MOVE COUNT TO WA_FINAL-X.

   MOVE 'X' TO WA_FINAL-Y.

   MOVE WA_ITAB-X TO WA_FINAL-Z.

   APPEND WA_FINAL TO IT_FINAL.

   CLEAR: WA_FINAL.

   MOVE COUNT TO WA_FINAL-X.

   MOVE 'Y' TO WA_FINAL-Y.

   MOVE WA_ITAB-Y TO WA_FINAL-Z.

   APPEND WA_FINAL TO IT_FINAL.

   CLEAR: WA_FINAL.

   MOVE COUNT TO WA_FINAL-X.

   MOVE 'Z' TO WA_FINAL-Y.

   MOVE WA_ITAB-Z TO WA_FINAL-Z.

   APPEND WA_FINAL TO IT_FINAL.

   CLEAR: WA_FINAL.

ENDLOOP.

LOOP AT IT_FINAL INTO WA_FINAL.

   WRITE:/ WA_FINAL-X,6 WA_FINAL-Y,9 WA_FINAL-Z.

ENDLOOP.


Regards,


Philip.

Read only

Former Member
0 Likes
1,278

Try below:


l_v_line = 0.

Loop at table_1 into wa_1.

l_v_line = l_v_line+1


wa_2-fld1 = l_v_line.

wa_2-fld2 = 'X'. "(Field_name1)
wa_2-fld3 = wa_1-fld1.
append wa_2 to table_2.

wa_2-fld2 = 'y'. "(Field_name2)
wa_2-fld3 = wa_1-fld2.
append wa_2 to table_2.

wa_2-fld2 = 'Z'. "(Field_name3)
wa_2-fld3 = wa_1-fld3.
append wa_2 to table_2.

clear wa_2.
Endloop.

Read only

0 Likes
1,277

HI Satish,

Thanks for your helpful answer it was correct i implemented the same logic.

But Iam not able to assign any points .

can anybody help me how can i assign points to this helpful answer.

Thanks,

smriti