admin管理员组

文章数量:1125582

Is there any way we can clear for loop structure fields in sap abap and return it back to new internal table.

  LOOP AT lt_a INTO DATA(ls_a).
    CLEAR: ls_a-f1, ls_a-f2.
    APPEND ls_a TO lt_n.
  ENDLOOP.

Can we achieve this scenario using FOR loop, Something like below?

lt_n = VALUE #( FOR <ls_a> IN LT_A ( f1 = '' f2 = '' ) ).

I need to send the complete structure <ls_a> with clear fields f1 and f2 to lt_n without mapping each field for <ls_a>.

Is there any way we can clear for loop structure fields in sap abap and return it back to new internal table.

  LOOP AT lt_a INTO DATA(ls_a).
    CLEAR: ls_a-f1, ls_a-f2.
    APPEND ls_a TO lt_n.
  ENDLOOP.

Can we achieve this scenario using FOR loop, Something like below?

lt_n = VALUE #( FOR <ls_a> IN LT_A ( f1 = '' f2 = '' ) ).

I need to send the complete structure <ls_a> with clear fields f1 and f2 to lt_n without mapping each field for <ls_a>.

Share Improve this question edited 2 days ago Sandra Rossi 13.6k6 gold badges24 silver badges55 bronze badges asked 2 days ago divScorpdivScorp 5021 gold badge9 silver badges18 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

I think this will do it, if the field names are the same:

lt_n = VALUE #( FOR <ls_a> IN lt_a 
              ( CORRESPONDING #( <ls_a> 
                  MAPPING f1 = DEFAULT VALUE #( ) 
                          f2 = DEFAULT VALUE #( ) ) ).

You may use BASE followed by the structured variable to set its initial value and followed by one or more field assignments to make the base vary. Documentation of BASE for structures: VALUE, Structures.

Demonstration:

TYPES: begin of ts_a,
         f0 TYPE string,
         f1 TYPE string,
       end of ts_a.
TYPES tt_a TYPE STANDARD TABLE OF ts_a WITH EMPTY KEY.

DATA(lt_a) = VALUE tt_a( ( f0 = 'F0' f1 = 'F1' ) ).

DATA(lt_n) = VALUE tt_a( FOR <ls_a> IN lt_a ( VALUE #( BASE <ls_a> f1 = '' ) ) ).

" Expected result:
ASSERT lt_n = VALUE tt_a( ( f0 = 'F0' f1 = '' ) ).

本文标签: