Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there something like MOVE-CORRESPONDING IGNORING INITIALS?

Tags:

abap

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?

like image 916
Jagger Avatar asked Feb 02 '12 20:02

Jagger


2 Answers

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.
like image 177
Jagger Avatar answered Sep 21 '22 23:09

Jagger


There is no language construct for arbitrary structures. For character fields, you can use OVERLAY ... WITH, but if you try to do this with structures, it leads to really messy code and lots of unforseen trouble with variable-length content. The best bet would be to use RTTI (Runtime Type Identification) to do this, but be careful when checking for initial values.

like image 38
vwegert Avatar answered Sep 22 '22 23:09

vwegert