ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Assignments → Assigning Structure Components → Assigning Components: Examples
CL_ABAP_CORRESPONDING for Nested Structures
This example demonstrates the system class CL_ABAP_CORRESPONDING for nested structures.
Other versions:
7.31 | 7.40 | 7.54
Source Code
DATA:
BEGIN OF struct1,
a1 TYPE string VALUE 'a1',
a2 TYPE string VALUE 'a2',
a3 TYPE string VALUE 'a3',
a4 TYPE string VALUE 'a4',
a5 TYPE string VALUE 'a5',
BEGIN OF asub1,
s1_a1 TYPE string VALUE 's1_a1',
s1_a2 TYPE string VALUE 's1_a2',
s1_a3 TYPE string VALUE 's1_a3',
BEGIN OF asub2,
s2_a1 TYPE string VALUE 's2_a1',
s2_a2 TYPE string VALUE 's2_a2',
s2_a3 TYPE string VALUE 's2_a3',
END OF asub2,
END OF asub1,
END OF struct1,
BEGIN OF struct2,
b1 TYPE string VALUE 'b1',
b2 TYPE string VALUE 'b2',
b3 TYPE string VALUE 'b3',
a4 TYPE string VALUE 'b4',
a5 TYPE string VALUE 'b5',
BEGIN OF bsub1,
s1_b1 TYPE string VALUE 's1_b1',
s1_b2 TYPE string VALUE 's1_b2',
s1_b3 TYPE string VALUE 's1_b3',
BEGIN OF bsub2,
s2_b1 TYPE string VALUE 's2_b1',
s2_b2 TYPE string VALUE 's2_b2',
s2_b3 TYPE string VALUE 's2_b3',
END OF bsub2,
END OF bsub1,
END OF struct2.
DATA(mapping_tab) = VALUE cl_abap_corresponding=>mapping_table(
( level = 0 kind = 1 srcname = 'a1' dstname = 'b3' )
( level = 0 kind = 1 srcname = 'a3' dstname = 'b1' )
( level = 0 kind = 2 srcname = 'a5' )
( level = 0 kind = 1 srcname = 'asub1' dstname = 'bsub1' )
( level = 1 kind = 1 srcname = 's1_a1' dstname = 's1_b3' )
( level = 1 kind = 1 srcname = 's1_a3' dstname = 's1_b1' )
( level = 1 kind = 1 srcname = 'asub2' dstname = 'bsub2' )
( level = 2 kind = 1 srcname = 's2_a1' dstname = 's2_b3' )
( level = 2 kind = 1 srcname = 's2_a3' dstname = 's2_b1' ) ).
cl_abap_corresponding=>create(
source = struct1
destination = struct2
mapping = mapping_tab
)->execute( EXPORTING source = struct1
CHANGING destination = struct2 ).
cl_demo_output=>new(
)->write( struct2-b1
)->write( struct2-b2
)->write( struct2-b3
)->write( struct2-a4
)->write( struct2-a5
)->write( struct2-bsub1-s1_b1
)->write( struct2-bsub1-s1_b2
)->write( struct2-bsub1-s1_b3
)->write( struct2-bsub1-bsub2-s2_b1
)->write( struct2-bsub1-bsub2-s2_b2
)->write( struct2-bsub1-bsub2-s2_b3
)->display( ).
Description
The components of the structure struct1
are assigned to the structure struct2
using the system class
CL_ABAP_CORRESPONDING.
- At the top level:
a1
is mapped tob3
anda3
is mapped tob1
.
a2
tob2
are ignored, since their names do not match andb2
keeps their value.
- The component
a4
appears in both structures and is assigned using matching names.
a5
also appears in both structures but is excluded explicitly by the mapping type.
- The substructure
asub1
is mapped to the substructurebsub1
.
- At the level of the first substructure:
s1_a1
is mapped tos1_b3
ands1_a3
is mapped tos1_b1
.
s1_a2
tos1_b2
are ignored, since their names do not match ands1_b2
keeps their value.
- The substructure
asub2
is mapped to the substructurebsub2
.
- At the level of the second substructure:
s2_a1
is mapped tos2_b3
ands2_a3
is mapped tos2_b1
.
s2_a2
tos2_b2
are ignored, since their names do not match ands2_b2
keeps their value.