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: 

COLLECT in ABAP 740

aasim_khan
Participant
0 Kudos

After posting a question on APPEND in ABAP 740, here I'm again on COLLECT statement.

Old Way:

METHOD meth_collect_old.

     CLEAR gw_mard.

     LOOP AT it_mard INTO DATA(lw_mard).

       gw_mard-matnr = lw_mard-matnr.

       gw_mard-labst = lw_mard-labst.

       COLLECT gw_mard INTO rt_col1.

       CLEAR gw_mard.

     ENDLOOP.

ENDMETHOD.

New Way:

METHOD meth_collect_new.

     DATA(lt_mard) = it_mard.

     SORT lt_mard BY matnr.

     DELETE ADJACENT DUPLICATES FROM lt_mard COMPARING matnr.

     rt_col2 = VALUE #( FOR <lf_mard> IN lt_mard

                        ( matnr = <lf_mard>-matnr

                          labst  = REDUCE #(

                                      INIT lv_sum = 0

                                      FOR lw_mard IN it_mard

                                              WHERE ( matnr = <lf_mard>-matnr )

                                      NEXT lv_sum = lv_sum + lw_mard-labst

                                  )

                        )

               ).

ENDMETHOD.

Got any better idea/solution for this? I feel the good old COLLECT statement is still better until something new is introduced.

1 ACCEPTED SOLUTION

SuhaSaha
Advisor
Advisor

Maybe you should refer to Horst Keller's response to my question here - .

Anyway, i would have used LOOP AT GROUP to calculate the sum per MATNR instead of using SORT & DAD!

6 REPLIES 6

SuhaSaha
Advisor
Advisor

Maybe you should refer to Horst Keller's response to my question here - .

Anyway, i would have used LOOP AT GROUP to calculate the sum per MATNR instead of using SORT & DAD!

0 Kudos

Well, let me give a try to LOOP AT GROUP. But I feel, COLLECT still retains the essence here

If we have to optimize more per the new syntax, then how about the below code? In both cases, COLLECT rules!

    LOOP AT it_mard INTO DATA(lw_mard).

       DATA(gw_mard) = VALUE ty_mard( matnr = lw_mard-matnr

                                                                        labst = lw_mard-labst ).

                                           

       COLLECT gw_mard INTO rt_col1.

       CLEAR gw_mard.

     ENDLOOP.


Or this


    LOOP AT it_mard INTO DATA(lw_mard).

       DATA(gw_mard) = CORRESPONDING ty_mard( lw_mard ).

       COLLECT gw_mard INTO rt_col1.

       CLEAR gw_mard.

     ENDLOOP.



0 Kudos

I suggested LOOP AT GROUP as an elegant substitute for SORT & DAD (see your original post). You can use COLLECT in conjunction with it, if you want.

0 Kudos

Yea, LOOP AT GROUP is another way! Below's the code... Not sure if it adheres the KISS principle, though.

LOOP AT it_mard INTO DATA(lw_mard)

                     GROUP BY lw_mard-matnr .

  DATA(lt_member) = VALUE tt_mard( ).

  LOOP AT GROUP lw_mard INTO DATA(ls_mard).

    lt_member = VALUE #( BASE lt_member ( ls_mard ) ).

  ENDLOOP.

  CLEAR gw_mard.

  LOOP AT lt_member INTO DATA(lw_member).

    gw_mard-matnr = lw_member-matnr.

    gw_mard-labst = gw_mard-labst + lw_member-labst.

  ENDLOOP.

  rt_col2 = VALUE #( BASE rt_col2 ( gw_mard ) ).

ENDLOOP.

0 Kudos

I don't understand why you have to build the member table manually?

LOOP AT GROUP is the member loop, you can use COLLECT in conjunction with it!

What I find interesting with grouping of int. tables is that you can sort the groups locally. In my case the original table BOOKINGS is sorted ascending wrt FLDATE but the groups are sorted descending wrt FLDATE.

BR,

Suhas

PS: Copy-Paste is not working on my IE11, hence attaching the screenshot & text files

0 Kudos

thanks