Skip to content

openABAP doesn't support the LOOP AT ... GROUP BY syntax #171

@jung-thomas

Description

@jung-thomas

Related to Exercism. Trying to use the LOOP AT ... GROUP BY syntax to solve an exercise. Here is an example the executes in an ABAP 7.51 and higher system but in open-abap produces a parsing error/ syntax error that there is no endloop.

CLASS zcl_itab_aggregation DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.
    TYPES  group TYPE c LENGTH 1.
    TYPES: BEGIN OF initial_numbers_type,
             group  TYPE group,
             number TYPE i,
           END OF initial_numbers_type,
           initial_numbers TYPE STANDARD TABLE OF initial_numbers_type WITH EMPTY KEY.

    TYPES: BEGIN OF aggregated_data_type,
             group   TYPE group,
             count   TYPE i,
             sum     TYPE i,
             min     TYPE i,
             max     TYPE i,
             average TYPE f,
           END OF aggregated_data_type,
           aggregated_data TYPE STANDARD TABLE OF aggregated_data_Type WITH EMPTY KEY.

  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.

CLASS zcl_itab_aggregation IMPLEMENTATION.
  METHOD if_oo_adt_classrun~main.
    DATA(initial_numbers) = VALUE initial_numbers(
        ( group = 'A' number = 10 )
        ( group = 'B' number = 5 )
        ( group = 'A' number = 6 )
        ( group = 'C' number = 22 )
        ( group = 'A' number = 13 )
        ( group = 'C' number = 500 )
    ).
    DATA aggregated_data TYPE aggregated_data.

    LOOP AT initial_numbers REFERENCE INTO DATA(initial_number)
       GROUP BY ( key = initial_number->group  count = GROUP SIZE )
       ASCENDING
       REFERENCE INTO DATA(group_key).

      APPEND INITIAL LINE TO aggregated_data REFERENCE INTO DATA(aggregated_item).
      aggregated_item->group = group_key->key.
      aggregated_item->count = group_key->count.
      aggregated_item->min = 9999999.
      LOOP AT GROUP group_key REFERENCE INTO DATA(group_item).
        aggregated_item->sum = aggregated_item->sum + group_item->number.
        aggregated_item->min = nmin( val1 = aggregated_item->min
                                     val2 = group_item->number ).
        aggregated_item->max = nmax( val1 = aggregated_item->max
                                     val2 = group_item->number ).
      ENDLOOP.
      aggregated_item->average = aggregated_item->sum / aggregated_item->count.
    ENDLOOP.

    out->write( initial_numbers ).
    out->write( aggregated_data ).

  ENDMETHOD.
ENDCLASS.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions