Skip to content

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

Component Operator, Mapping Rule

This example demonstrates the component operator with explicit mapping rules.

Other versions: 7.31 | 7.40 | 7.54

Source Code

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

    out->begin_section( `struct1`
      )->write( struct1
      )->next_section( `struct2`
      )->write( struct2 ).

    struct2 = CORRESPONDING #( struct1 ).
    out->begin_section(
      `struct2 = CORRESPONDING #( struct1 )`
     )->write( struct2 ).

    struct2 = CORRESPONDING #( struct1 MAPPING b4 = a3 ).
    out->begin_section(
      `struct2 = CORRESPONDING #( struct1 MAPPING b4 = a3 )`
     )->write( struct2 ).

    struct2 = CORRESPONDING #( struct1 MAPPING b4 = a1 ).
    out->begin_section(
      `struct2 = CORRESPONDING #( struct1 MAPPING b4 = a1 )`
     )->write( struct2 ).

    struct2 = CORRESPONDING #( struct1 EXCEPT a1 ).
    out->begin_section(
      `struct2 = CORRESPONDING #( struct1 EXCEPT a1 )`
     )->write( struct2 ).

    struct2 = CORRESPONDING #( struct1  MAPPING b4 = a3 EXCEPT a1 ).
    out->begin_section(
      `struct2 = CORRESPONDING #( struct1 MAPPING b4 = a3 EXCEPT a1 )`
     )->write( struct2 ).

    struct2 = CORRESPONDING #( struct1  MAPPING b4 = a3 EXCEPT * ).
    out->begin_section(
      `struct2 = CORRESPONDING #( struct1 MAPPING b4 = a3 EXCEPT * )`
     )->write( struct2 ).

    struct2 = CORRESPONDING #( struct1 EXCEPT * ).
    out->begin_section(
      `struct2 = CORRESPONDING #( struct1 EXCEPT * )`
     )->write( struct2 ).

    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.

  • If no mapping rule is used, the identically named components a1 and a2 are assigned and the other components remain initial.
  • The mapping rule MAPPING b4 = a3 assigns the component a3 to the component b4 too.
  • The mapping rule MAPPING b4 = a1 assigns the component a1 twice, to b4 and to the identically named component a1.
  • The mapping rule EXCEPT a1 assigns only the component a2 to the identically named component, since the component a1 is excluded.
  • Alongside the identical name assignment, the mapping rule MAPPING b4 = a3 EXCEPT a1 assigns the component a3 to the component b4 and a1 is excluded from the identical name assignment.
  • The mapping rule EXCEPT * does not assign any components and all components of the result remain initial.