Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

"Compact/Compress" internal table

Former Member
0 Kudos
1,160

I have an internal table that has two line items per records where the first line item has data in some fields and the second has data in the other fields. What I want is to put these two line items into one so that all fields are filled, in one line. I started coding the following but I then realized that move-corresponding will overwrite with NULL values. Basically I need to do a MOVE-CORRESPONDING when the source field is not NULL, or, when the target field is NULL. Is there a way to do this?

  SORT it_out BY belnr.

  CLEAR: d_tabix, d_index.
  LOOP AT it_out INTO wa_out.
    ADD 1 TO d_tabix.
    AT END OF belnr.
      d_index = d_tabix - 1.
      READ TABLE it_out INTO wa_out2 INDEX d_index.
      MOVE-CORRESPONDING wa_out2 TO wa_out.
      MODIFY it_out FROM wa_out.
      DELETE it_out INDEX d_index.
      SUBTRACT 1 FROM d_tabix.
    ENDAT.
  ENDLOOP.

Regards,

Davis

1 ACCEPTED SOLUTION

Former Member
0 Kudos
205

Two thoughts:

1 - move the fields individually.

2 - if you must use MOVE-CORRESPONDING, create another work area with just the second set of fields, then use MOVE-CORRESPONDING twice to move the data first from the initial table to the new work area and then the new work area to the final table. This would be more cumbersome and perform less well.

Rob

3 REPLIES 3

Former Member
0 Kudos
206

Two thoughts:

1 - move the fields individually.

2 - if you must use MOVE-CORRESPONDING, create another work area with just the second set of fields, then use MOVE-CORRESPONDING twice to move the data first from the initial table to the new work area and then the new work area to the final table. This would be more cumbersome and perform less well.

Rob

0 Kudos
205

Rob,

I was trying to avoid moving the fields one by one; I guess I will have to do that though.

Thanks,

Davis

Former Member
205

Hi,

1)

you can move one by one..

2) Use the function module get_component_list to get the components of the source structure and move it using field-symbols..

Example code..

DATA: wa_mara TYPE mara.
DATA: wa_vbap TYPE vbap.
DATA: t_comp  TYPE STANDARD TABLE OF rstrucinfo.
DATA: wa_comp TYPE rstrucinfo.
FIELD-SYMBOLS: <fs_mara> TYPE ANY,
               <fs_vbap> TYPE ANY.

* get the components.
CALL FUNCTION 'GET_COMPONENT_LIST'
  EXPORTING
    program    = sy-repid
    fieldname  = 'WA_MARA'
  TABLES
    components = t_comp.

wa_mara-matnr = 'test'.

* Move from wa_mara to wa_vbap with the common field name and only if
* is populated.
LOOP AT t_comp INTO wa_comp.

* GEt the mara value.
  ASSIGN COMPONENT wa_comp-compname OF STRUCTURE wa_mara TO <fs_mara>.

  CHECK sy-subrc = 0.

* GEt the vbap value.
  ASSIGN COMPONENT wa_comp-compname OF STRUCTURE wa_vbap TO <fs_vbap>.

  CHECK sy-subrc = 0.

* If the source is populated the only move.
  IF NOT <fs_mara>  IS INITIAL.

    <fs_vbap> = <fs_mara>.

  ENDIF.

ENDLOOP.

WRITE: / wa_vbap-matnr.

THanks

Naren