Wednesday, April 25, 2012

Convert Internal Table Rows to Columns and Columns to Rows



TYPES:  BEGIN OF ty_col_row,
         col01 TYPE string,
         col02 TYPE string,
         col03 TYPE string,
         END OF ty_col_row.
 FIELD-SYMBOLS:  <fs1> TYPE any,
                 <fs2> TYPE any.
 DATA: it1_col_row TYPE STANDARD TABLE OF ty_col_row,
       wa1_col_row TYPE ty_col_row,
       it2_col_row TYPE STANDARD TABLE OF ty_col_row,
       wa2_col_row TYPE ty_col_row,
       cline   TYPE sy-tabix.
 wa1_col_row-col01 = 'C-1_R-1'.  wa1_col_row-col02 = 'C-2_R-1'.  wa1_col_row-col03 = 'C-1_R-1'.
 APPEND wa1_col_row TO it1_col_row.
 wa1_col_row-col01 = 'C-1_R-2'.  wa1_col_row-col02 = 'C-2_R-2'.  wa1_col_row-col03 = 'C-1_R-2'.
 APPEND wa1_col_row TO it1_col_row.
 wa1_col_row-col01 = 'C-1_R-3'.  wa1_col_row-col02 = 'C-2_R-3'.  wa1_col_row-col03 = 'C-1_R-3'.
 APPEND wa1_col_row TO it1_col_row.
 LOOP AT it1_col_row INTO wa1_col_row.
   WRITE: / wa1_col_row-col01, wa1_col_row-col02, wa1_col_row-col03 .
 ENDLOOP.
 WRITE: /.
 LOOP AT it1_col_row INTO wa1_col_row.
   CLEAR wa2_col_row.
   ASSIGN COMPONENT sy-tabix OF STRUCTURE wa2_col_row TO <fs1>.
   cline = sy-tabix.
   DO.
     ASSIGN COMPONENT sy-index OF STRUCTURE wa1_col_row TO <fs2>.
     IF sy-subrc NE 0.
       EXIT.
     ENDIF.
     IF cline = 1.
       <fs1> = <fs2>.
       APPEND wa2_col_row TO it2_col_row.
     ELSE.
       READ TABLE it2_col_row INTO wa2_col_row INDEX sy-index.
       <fs1> = <fs2>.
       MODIFY it2_col_row FROM wa2_col_row INDEX sy-index.
     ENDIF.
   ENDDO.
 ENDLOOP.
 LOOP AT it2_col_row INTO wa2_col_row.
   WRITE: / wa2_col_row-col01, wa2_col_row-col02, wa2_col_row-col03 .
 ENDLOOP.

1 comment: