Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ITAB-Aggregation error when using FOR .. IN GROUP with DATA REFERENCE #280

Open
brehberg opened this issue Aug 24, 2023 · 2 comments
Open

Comments

@brehberg
Copy link

I ran into the following error when attempting to solve ITAB Aggregation exercise. It works on ABAP AS, but not on exercism.
image

I was using LOOP .. GROUP BY .. REFERENCE INTO DATA(key) and then had a nested FOR expression with FOR .. IN GROUP key

It did work after changing the group_result to simply use INTO DATA(key) instead of the data reference.
Here is a link to my published solutions on Exercism: https://exercism.org/tracks/abap/exercises/itab-aggregation/solutions/brehberg

@larshp
Copy link
Member

larshp commented Aug 24, 2023

thanks, please post the full code that gives the error in this issue

@brehberg
Copy link
Author

brehberg commented Aug 24, 2023

CLASS zcl_itab_aggregation DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    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.

    METHODS perform_aggregation
      IMPORTING
        initial_numbers        TYPE initial_numbers
      RETURNING
        VALUE(aggregated_data) TYPE aggregated_data.
  PROTECTED SECTION.
  PRIVATE SECTION.

ENDCLASS.



CLASS zcl_itab_aggregation IMPLEMENTATION.

  METHOD perform_aggregation.

    LOOP AT initial_numbers REFERENCE INTO DATA(record)
      GROUP BY ( group = record->group
                 size = GROUP SIZE
                ) REFERENCE INTO DATA(key).

      DATA(aggregate) = REDUCE aggregated_data_type(
        INIT result = VALUE #(
            group = key->group
            count = key->size
            min = cl_abap_math=>max_int4
            max = cl_abap_math=>min_int4 )
        FOR member IN GROUP key
        NEXT result = VALUE #( BASE result
          sum = result-sum + member-number
          min = nmin( val1 = result-min val2 = member-number )
          max = nmax( val1 = result-max val2 = member-number )
        ) ).

      APPEND VALUE #( BASE aggregate
        average = aggregate-sum / aggregate-count
      ) TO aggregated_data.
    ENDLOOP.

  ENDMETHOD.

ENDCLASS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants