cancel
Showing results for 
Search instead for 
Did you mean: 

i need nested for loop for deep structure

aravindcsebe
Explorer
0 Kudos
method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_EXPANDED_ENTITYSET.

    DATA :
      IT_DEEP TYPE STANDARD TABLE OF ZDEEP_S,
           WA_deep TYPE ZDEEP_S,
      IT_HEAD TYPE STANDARD TABLE OF ZHEADER,
           WA_HEAD TYPE ZHEADER,
      IT_ITEM TYPE STANDARD TABLE OF ZITEM1,

           WA_ITEM TYPE ZITEM1.


    CASE iv_entity_set_name.

      WHEN 'headerSet'.

        SELECT * FROM ZHEADER INTO TABLE IT_HEAD.
        IF SY-SUBRC EQ 0.
          SELECT * FROM ZITEM1 INTO TABLE IT_ITEM FOR ALL ENTRIES IN IT_HEAD WHERE ID EQ IT_HEAD-ID.

        ENDIF.


    ENDCASE.


    LOOP AT IT_HEAD INTO WA_HEAD.

      MOVE-CORRESPONDING wa_head to wa_deep.

      loop at IT_ITEM into WA_ITEM.

        if WA_HEAD-ID eq WA_ITEM-ID.

          APPEND WA_ITEM to WA_DEEP-HEADERTOITEMNAV.
          CLEAR WA_ITEM.

        ENDIF.






      ENDLOOP.
      APPEND WA_DEEP to IT_DEEP.
      clear WA_DEEP.
    ENDLOOP.







    COPY_DATA_TO_REF(
      exporting
        IS_DATA = IT_DEEP
      changing
        CR_DATA = ER_ENTITYSET
    ).

  endmethod.





in this code i have used nested loop in that place i need nested for loop
FredericGirod
Active Contributor

Could you use [CODE] button to format your code, and make some sentences to explain what do you want to do and what is your problem ?

View Entire Topic
Sandra_Rossi
Active Contributor

I hope that you will ask some questions about the code below, so that you understand, and next time you will be able to find by yourself.

Old code:

    LOOP AT IT_HEAD INTO WA_HEAD.
      MOVE-CORRESPONDING wa_head to wa_deep.
      loop at IT_ITEM into WA_ITEM.
        if WA_HEAD-ID eq WA_ITEM-ID.
          APPEND WA_ITEM to WA_DEEP-HEADERTOITEMNAV.
          CLEAR WA_ITEM.
        ENDIF.
      ENDLOOP.
      APPEND WA_DEEP to IT_DEEP.
      clear WA_DEEP.
    ENDLOOP.

New code (I couldn't check the syntax because you didn't provide a reproducible example):

    it_deep = VALUE #( FOR <wa_head> IN it_head
                       ( VALUE #( BASE CORRESPONDING #( <wa_head> )
                         HeaderToItemNav = VALUE #( FOR <wa_item> IN it_item
                                                    WHERE ( id = <wa_head>-id )
                                                    ( <wa_item> ) ) ) ) ).
aravindcsebe
Explorer
0 Kudos

this is working fine, as I expected.

how did you do that you are a gem. 🙂

aravindcsebe
Explorer
0 Kudos

if you have time please give us some explanation about the new syntax, because i did not understand that.

FredericGirod
Active Contributor

You have old and new code, you could understand.

VALUE type( field1 = .... field 2 = .. ). <-- set data into a structure with one statement

FOR my_field IN my_data <-- more or less like a LOOP where my_field get value of the field of my_data one by one

my_result = CORRESPONDING output_structure( input_structure ) <-- little bit like move-corresponding with additionnal tool

aravindcsebe
Explorer
0 Kudos

VALUE #( BASE CORRESPONDING #( wa_head1 )

this is what I didn't understand

Sandra_Rossi
Active Contributor

These are two different parts that you can analyze separately and in order:

  1. CORRESPONDING #( wa_head1 ) will be like a move-corresponding wa_head1 to a temporary "anonymous" structure (no name indicated)
  2. VALUE #( BASE base_structure comp1 = ... comp2 = ... ) says that the structure after BASE will have some of its components initialized with other values, and the components which are not mentioned keep their values coming from the base structure