01-31-2023 3:54 PM
Dear all,
A question which I need to raise in order to just clear up my ideas, since I am already achieving what I need using the powerful LOOP AT ... GROUP BY.
I have the following code:
LOOP AT internal_table INTO DATA(record)
GROUP BY ( criteria = record-somefield )
ASSIGNING FIELD-SYMBOL(<group>).
"some code here
LOOP AT GROUP <group> ASSIGNING FIELD-SYMBOL(<group_member>).
"other code: here we can modify the group member using <group_member>
ENDLOOP.
ENDLOOP.
I cannot understand the reason to declare the variable "record", in the external part of the loop: it stays initial for the whole time.
I checked several blogs and documentation, so I ask in case somebody knows.
Thanks a lot in advance for your precious help!
02-01-2023 11:03 AM
The declaration is used for grouping. In GROUP BY, the line to be examined should be usable in expressions of any complexity, e.g. method call.
This is not possible without binding to a symbol.
If a group symbol is defined, the variable is meaningless after grouping and becomes unbound.
If no group symbol is defined, it takes a representative of the group in each iteration.
Documented in the ABAP Keyword Documentation here.
01-31-2023 8:02 PM
I hope SAP experts will answer that question.
I think it's because the old compiler has been reused or almost unchanged, so it's more or less transpiling to ABAP byte code via classic ABAP, it was probably easier to ask the developer to declare that intermediate variable.
01-31-2023 10:55 PM
At least in representative binding, the variable "record" contains the first line of each group in the loop:
LOOP AT internal_table INTO DATA(record) GROUP BY record-somefield.
… record-somefield ...
ENDLOOP.
02-01-2023 7:03 AM
easier and more legible maybe, because I personally don't see a better alternative.
02-01-2023 11:03 AM
The declaration is used for grouping. In GROUP BY, the line to be examined should be usable in expressions of any complexity, e.g. method call.
This is not possible without binding to a symbol.
If a group symbol is defined, the variable is meaningless after grouping and becomes unbound.
If no group symbol is defined, it takes a representative of the group in each iteration.
Documented in the ABAP Keyword Documentation here.
02-01-2023 12:44 PM
Thank you so much Andrea for taking time to answer and for the excellent answer, which made me understand. 🙂
Here is a little code I tried to test what I understood from you:
REPORT test.
CLASS lcl_report DEFINITION.
PUBLIC SECTION.
TYPES BEGIN OF idx_val.
TYPES idx TYPE i.
TYPES val TYPE string.
TYPES END OF idx_val.
TYPES idx_val_tab TYPE STANDARD TABLE OF idx_val WITH DEFAULT KEY.
METHODS main.
METHODS even_odd IMPORTING idx TYPE i
RETURNING VALUE(result) TYPE string.
ENDCLASS.
CLASS lcl_report IMPLEMENTATION.
METHOD main.
DATA(table) = VALUE idx_val_tab( ( idx = 1 val = 'one' )
( idx = 2 val = 'two' )
( idx = 3 val = 'three' )
( idx = 4 val = 'four' )
( idx = 5 val = 'five' )
( idx = 6 val = 'six' ) ).
LOOP AT table INTO DATA(record)
GROUP BY ( index_key = even_odd( idx = record-idx ) )
ASSIGNING FIELD-SYMBOL(<group>).
WRITE: / '<group>-index_key:', <group>-index_key.
LOOP AT GROUP <group> ASSIGNING FIELD-SYMBOL(<group_member>).
WRITE: / '<group_member>', <group_member>-idx.
ENDLOOP.
ENDLOOP.
ENDMETHOD.
METHOD even_odd.
WRITE: / 'METHOD even_or_zero'.
WRITE: 'idx: ', idx.
IF idx MOD 2 EQ 0.
result = 'even'.
ELSE.
result = 'odd'.
ENDIF.
WRITE: 'result: ', result.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(report) = NEW lcl_report( ).
report->main( ).
02-01-2023 1:26 PM
02-01-2023 11:47 AM
Maybe I "overengineered" your question 😄
even SAP people don't explain how the syntax was designed (for now)
(if the question was only about the reason why it's initial, Andrea answered)