Is there something like MOVE-CORRESPONDING IGNORING INITIALS in ABAP?

Jagger picture Jagger · Feb 2, 2012 · Viewed 7.5k times · Source

I am looking for a language construct or a function module which would be MOVE-CORRESPONDING IGNORING INITIALS like. Simply put I want something that works exactly like MOVE-CORRESPONDING source TO dest but ignoring all the fields which are initial in the source.

Is there someting like that?

Answer

Jagger picture Jagger · Feb 3, 2012

I have prepared my own piece of code that I want to share. It is not perfect, it will not work with complex structures. However I do not need anything more right now than to work on flat structures.

CLASS lcl_utilities DEFINITION FINAL CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-METHODS:
      move_corresponding_ignore_init
        IMPORTING
          i_str_source TYPE any
        CHANGING
          c_str_dest   TYPE any.
ENDCLASS.

CLASS lcl_utilities IMPLEMENTATION.
  METHOD move_corresponding_ignore_init.
    DATA:
      l_rcl_abap_structdescr TYPE REF TO cl_abap_structdescr.

    l_rcl_abap_structdescr ?= cl_abap_typedescr=>describe_by_data( i_str_source ).
    LOOP AT l_rcl_abap_structdescr->components ASSIGNING FIELD-SYMBOL(<fs_str_component>).
      ASSIGN COMPONENT <fs_str_component>-name OF STRUCTURE c_str_dest TO FIELD-SYMBOL(<fs_dest_field>).
      IF sy-subrc = 0.
        ASSIGN COMPONENT <fs_str_component>-name OF STRUCTURE i_str_source TO FIELD-SYMBOL(<fs_source_field>).
        ASSERT sy-subrc = 0.
        IF <fs_source_field> IS NOT INITIAL.
          <fs_dest_field> = <fs_source_field>.
        ENDIF.
      ENDIF.
    ENDLOOP.
  ENDMETHOD.                    "move_corresponding_ignore_init
ENDCLASS.

...and a small macro in order to use it more less like a language construct.

DEFINE move_corresponding_ignore_init.
  lcl_utilities=>move_corresponding_ignore_init(
    exporting
      i_str_source = &1
    changing
      c_str_dest   = &2
  ).
END-OF-DEFINITION.