Use of CL_ABAP_CORRESPONDING with internal table

Ne1kFo1465660380CL_ABAP_CORRESPONDING class provides simple methods for field mapping between internal  tables.

 

 

CODE

DATABEGIN OF line1,
                 col1 TYPE STRING,
                  col2 TYPE STRING,
             END OF line1,
             BEGIN OF line2,
                   col2 TYPE STRING,
                   col3 TYPE STRING,
              END OF line2.

DATAitab1 LIKE TABLE OF line1 WITH EMPTY KEY,
               itab2 LIKE TABLE OF line2 WITH EMPTY KEY.

itab1 VALUE #(
                                  col1 ‘A1’ col2 ‘A2’ )
                                  col1 ‘B1’ col2 ‘B2’ ).

itab2 VALUE #(
                                col2 ‘X1’ col3 ‘X2’ )
                                col2 ‘Y1’ col3 ‘Y2’ ).
WRITE:‘ITAB1 CONTENT’ COLOR 3.
WRITE:/1 ‘COL1’10 ‘COL2’.
LOOP AT itab1 INTO line1.
  WRITE:/1 line1col110 line1col2.
ENDLOOP.
WRITE:‘ITAB2 CONTENT’ COLOR 3.
WRITE:/1 ‘COL2’10 ‘COL3’.
LOOP AT itab2 INTO line2.
  WRITE:/1 line2col2,10 line2col3.
ENDLOOP.

” Define Mapping table
DATA(mapper=  cl_abap_corresponding=>create(
                                  source  itab1
                                  destination itab2
                                  mapping VALUE cl_abap_corresponding=>mapping_table).

mapper->executeEXPORTING source itab1
                                    CHANGING  destination itab2 ).
CLEARline2.
WRITE:‘ITAB2 CONTENT AFTER’ COLOR 3.
WRITE:/1 ‘COL2’10 ‘COL3’.
LOOP AT itab2 INTO line2.
  WRITE:/1 line2col210 line2col3.
ENDLOOP.


OUTPUT

5.png


DATABEGIN OF line1,
                 col1 TYPE STRING,
                  col2 TYPE STRING,
             END OF line1,
             BEGIN OF line2,
                   col2 TYPE STRING,
                   col3 TYPE STRING,
              END OF line2.

DATAitab1 LIKE TABLE OF line1 WITH EMPTY KEY,
              itab2 LIKE TABLE OF line2 WITH EMPTY KEY.

itab1 VALUE #(
                               col1 ‘A1’ col2 ‘A2’ )
                               col1 ‘B1’ col2 ‘B2’ ).

itab2 VALUE #(
                                col2 ‘X1’ col3 ‘X2’ )
                                col2 ‘Y1’ col3 ‘Y2’ ).
WRITE:‘ITAB1 CONTENT’ COLOR 3.
WRITE:/1 ‘COL1’10 ‘COL2’.
LOOP AT itab1 INTO line1.
  WRITE:/1 line1col110 line1col2.
ENDLOOP.
WRITE:‘ITAB2 CONTENT’ COLOR 3.
WRITE:/1 ‘COL2’10 ‘COL3’.
LOOP AT itab2 INTO line2.
  WRITE:/1 line2col2,10 line2col3.
ENDLOOP.

” Define mapping table
DATA(mapper=    cl_abap_corresponding=>create(
                               source      itab1
                               destination itab2
                                mapping    VALUE cl_abap_corresponding=>mapping_table(
                                             level kind srcname ‘col1’ dstname ‘col2’ )
                                             level kind srcname ‘col2’ dstname ‘col3’ ).

mapper->executeEXPORTING source itab1
                 CHANGING  destination itab2 ).
CLEARline2.
WRITE:‘ITAB2 CONTENT AFTER’ COLOR 3.
WRITE:/1 ‘COL2’10 ‘COL3’.
LOOP AT itab2 INTO line2.
  WRITE:/1 line2col210 line2col3.
ENDLOOP.


OUTPUT

6


 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s