Reputation: 1400
Currently, I have this coding, and it works perfectly fine:
TYPES: BEGIN OF tty_ekpo,
ebeln TYPE ebeln,
ebelp TYPE ebelp,
END OF tty_ekpo.
DATA: lt_ekpo TYPE TABLE OF tty_ekpo,
ls_ekpo LIKE LINE OF lt_ekpo.
LOOP AT gt_loopdata ASSIGNING FIELD-SYMBOL(<fs_collect>).
ls_ekpo-ebeln = <fs_collect>-ebeln.
ls_ekpo-ebelp = <fs_collect>-ebelp.
COLLECT ls_ekpo INTO lt_ekpo.
ENDLOOP.
I want to do the same with the new syntax, is it possible?
If yes, how?
Upvotes: 2
Views: 28069
Reputation: 1
Collects works perfect to SUM all numeric values un a table and reapecting all character fields.
loop at itable into structure. collect structure to Itable2. endcollect.
sadly this is being discontinued. (ATC check P1)
Upvotes: 0
Reputation: 13711
Your example doesn't deserve a collect
whose main interest is to sum numbers, and your internal table doesn't contain numbers (only two character-like fields ebeln
and ebelp
).
Your example removes the duplicates, it could be possibly equivalent to a sort
and delete adjacent duplicates
, or to an insert
into an internal table with a unique key.
The term "new syntax" is unclear because each version brings a new syntax, but based on your own answer, you mean the use of Constructor Expressions which appeared in 2013 with ABAP 7.40.
In your case, there's no interest to use Constructor Expressions because it will just be more complex to understand and won't give a better performance.
Remember that collect
is not obsolete, except that you should use sorted/hashed tables instead of standard tables as explained in the "hints" of the collect
documentation (ABAP 7.58):
The statement COLLECT is not suitable for standard tables and should no longer be used for them. COLLECT can be used for sorted tables and hashed tables without any problems since these, unlike standard tables, always have their own stable key administration that can be exploited by COLLECT
Upvotes: 0
Reputation: 312
To answer my own question the result will be something line this:
TYPES: BEGIN OF ty_ekpo,
ebeln TYPE ebeln,
ebelp TYPE ebelp,
netwr TYPE ekpo-netwr,
matnr type ekpo-matnr,
END OF ty_ekpo,
tty_ekpo TYPE STANDARD TABLE OF ty_ekpo WITH EMPTY KEY.
DATA: it_ekpo TYPE SORTED TABLE OF ty_ekpo WITH UNIQUE KEY ebeln ebelp.
DATA(lt1_ekpo) = VALUE tty_ekpo(
FOR GROUPS <group_key> OF <g> IN it_ekpo GROUP BY ( ebeln = <g>-ebeln ebelp = <g>-ebelp )
LET coll_line = REDUCE #( INIT line TYPE ty_ekpo FOR <m> IN GROUP <group_key>
NEXT line-ebeln = <m>-ebeln
line-ebelp = <m>-ebelp
line-netwr = line-netwr + <m>-netwr
line-matnr = <m>-matnr )
IN ( coll_line ) ) .
Upvotes: 0
Reputation: 10621
Well, I disagree with the Joszsef variant, because addition WITHOUT MEMBERS
produces all group key values, it does NOT sum numeric fields automatically alike COLLECT
. You need additional actions for this.
Here is the enhanced Joszef's variant that fits your needs and collects NETWR field:
TYPES: BEGIN OF ty_ekpo,
ebeln TYPE ebeln,
ebelp TYPE ebelp,
netwr TYPE ekpo-netwr,
END OF ty_ekpo,
tty_ekpo TYPE STANDARD TABLE OF ty_ekpo WITH EMPTY KEY.
DATA: it_ekpo TYPE SORTED TABLE OF ty_ekpo WITH UNIQUE KEY ebeln ebelp.
DATA(lt1_ekpo) = VALUE tty_ekpo(
FOR GROUPS <group_key> OF <g> IN it_ekpo GROUP BY ( ebeln = <g>-ebeln ebelp = <g>-ebelp )
LET coll_line = REDUCE #( INIT line TYPE ty_ekpo FOR <m> IN GROUP <group_key>
NEXT line-ebeln = <m>-ebeln line-ebelp = <m>-ebelp line-netwr = line-netwr + <m>-netwr )
IN ( coll_line ) ) .
Another flavor based on two REDUCEs:
DATA(lt2_ekpo) = REDUCE tty_ekpo( INIT cline = VALUE tty_ekpo( )
FOR GROUPS <group_key> OF <g> IN it_ekpo GROUP BY ( ebeln = <g>-ebeln ebelp = <g>-ebelp )
NEXT cline = VALUE #( BASE cline ( ebeln = <group_key>-ebeln ebelp = <group_key>-ebelp
netwr = REDUCE netwr( INIT val TYPE netwr
FOR wa IN
FILTER #( it_ekpo WHERE ebeln = <group_key>-ebeln AND ebelp = <group_key>-ebelp )
NEXT val = val + wa-netwr ) ) ) ).
I see that in your original post you also do not make summarizing, you just collecting key fields into table. If this is what you need, Joszef snippet is OK. Just to notice that COLLECT
does more than that.
Upvotes: 5
Reputation: 5071
I use this one:
lt_ekpo = VALUE #( FOR GROUPS ebelnebelp OF <ls_collect> IN gt_lopdata
GROUP BY ( ebeln = <ls_collect>-ebeln
ebelp = <ls_collect>-ebelp )
ASCENDING WITHOUT MEMBERS ( ebelnebelp ) ).
Upvotes: 5