6

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?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Jagger
  • 10,350
  • 9
  • 51
  • 93

2 Answers2

3

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.
Jagger
  • 10,350
  • 9
  • 51
  • 93
1

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.

Lilienthal
  • 4,327
  • 13
  • 52
  • 88
vwegert
  • 18,371
  • 3
  • 37
  • 55
  • Thanks for the hint but it is not what I am looking for. I have prepared my own piece of code which is not so complex, I believe. – Jagger Feb 03 '12 at 13:05
  • @Jagger: Using RTTI is exactly what you did in the code you provided, so why would this not be what you're looking for? – vwegert Feb 05 '12 at 12:20
  • Sorry, maybe my answer was a little bit misunderstanding. I was taking about `OVERLAY WITH`. The second part of your answer is indeed what I used in my code. – Jagger Feb 05 '12 at 16:00