Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Assignments →  Assigning Components →  Assigning Components: Examples 

Component Operator, Nested Mapping Rule

This example demonstrates the component operator with a nested mapping rule.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA(out) = cl_demo_output=>new( ).

    out->begin_section( `struct1`
      )->write( CORRESPONDING out1( struct1 )
      )->write( struct1-istruct
      )->write( struct1-itab ).

    struct2 = CORRESPONDING #( struct1 ).
    out->next_section(
      `struct2 = CORRESPONDING #( struct1 )`
      )->write( CORRESPONDING out2( struct2 )
      )->write( struct2-istruct
      )->write( struct2-jtab ).

    struct2 = CORRESPONDING #( struct1 MAPPING jtab = itab ).
    out->next_section(
      `struct2 = CORRESPONDING #( struct1 MAPPING jtab = itab )`
      )->write( CORRESPONDING out2( struct2 )
      )->write( struct2-istruct
      )->write( struct2-jtab ).

    struct2 = CORRESPONDING #( struct1 MAPPING
               ( istruct = istruct MAPPING b1 = a1 EXCEPT a2 )
               ( jtab = itab MAPPING b1 = a1 ) ).
    out->next_section(
      `struct2 = CORRESPONDING #( struct1  MAPPING ( ... ) )`
      )->write( CORRESPONDING out2( struct2 )
      )->write( struct2-istruct
      )->write( struct2-jtab ).

    out->display( ).

Description

The example assigns the result of a constructor expression with the component operator CORRESPONDING that uses the components of an argument struct1 to a compatible structure struct2. Various mapping rules are demonstrated. The structures contain a substructure and a tabular component.

  • If there is not mapping rule, the identically named components a2 and istruct are assigned at the top level. The substructure istruct is resolved and the component a2 is assigned here. The tabular components are not identically named and are not assigned.
  • The mapping rule MAPPING jtab = itab is used to assign the tabular component too. Here, only the identically named column a2 is respected.
  • The nested mapping rule
MAPPING ( istruct = istruct MAPPING b1 = a1 EXCEPT a2 )
        ( jtab = itab MAPPING b1 = a1 )
is used to
  • specify a mapping relationship for the components of the substructure istruct. Here, istruct = istruct must be specified at the top level.
  • define a mapping for the tabular components. Here, a mapping is also nested for their columns.
In the result, all components are filled except the component a2 of the substructure, excluded using EXCEPT.