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

Variable Naming inside Reduce #200

Closed
ceedee666 opened this issue Sep 23, 2022 · 5 comments
Closed

Variable Naming inside Reduce #200

ceedee666 opened this issue Sep 23, 2022 · 5 comments
Assignees

Comments

@ceedee666
Copy link

Hi @larshp,

while working on the ITAB Aggreation exercise on Exercism I encounterd the follow issue.

The code below works on ABAP in the cloud.

CLASS zcl_itab_aggregation IMPLEMENTATION.
  METHOD perform_aggregation.
    LOOP AT initial_numbers INTO DATA(number)
        GROUP BY number-group
        ASSIGNING FIELD-SYMBOL(<group>).

      DATA(group_members) = VALUE initial_numbers( FOR member IN GROUP <group> ( member ) ).

      aggregated_data = VALUE #( BASE aggregated_data
                               ( group = group_members[ 1 ]-group
                                 count = lines( group_members )
                                 sum = REDUCE i( INIT s = 0 FOR g IN group_members NEXT s = s + g-number )
                                 min = REDUCE i( INIT min = 1000000 FOR g IN group_members NEXT min = COND #( WHEN g-number < min THEN g-number ELSE min ) )
                                 max = REDUCE i( INIT max = 0 FOR g IN group_members NEXT max = COND #( WHEN g-number > max THEN g-number ELSE max ) )
                                 average = REDUCE i( INIT s = 0 FOR g IN group_members NEXT s = s + g-number ) / lines( group_members )
                                 ) ).
    ENDLOOP.
  ENDMETHOD.

  METHOD if_oo_adt_classrun~main.
    DATA(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) = NEW zcl_itab_aggregation(  )->perform_aggregation( numbers ).

  ENDMETHOD.

ENDCLASS.

On Exercism I had to rename the variable names inside the REDUCE statments to be uniqe.

CLASS zcl_itab_aggregation IMPLEMENTATION.
  METHOD perform_aggregation.
    LOOP AT initial_numbers INTO DATA(number)
        GROUP BY number-group
        ASSIGNING FIELD-SYMBOL(<group>).

      DATA(group_members) = VALUE initial_numbers( FOR member IN GROUP <group> ( member ) ).

      aggregated_data = VALUE #( BASE aggregated_data
                               ( group = group_members[ 1 ]-group
                                 count = lines( group_members )
                                 sum = REDUCE i( INIT s = 0 FOR g IN group_members NEXT s = s + g-number )
                                 min = REDUCE i( INIT min = 1000000 FOR g1 IN group_members NEXT min = COND #( WHEN g1-number < min THEN g1-number ELSE min ) )
                                 max = REDUCE i( INIT max = 0 FOR g2 IN group_members NEXT max = COND #( WHEN g2-number > max THEN g2-number ELSE max ) )
                                 average = REDUCE i( INIT s1 = 0 FOR g3 IN group_members NEXT s1 = s1 + g-3number ) / lines( group_members )
                                 ) ).
    ENDLOOP.
  ENDMETHOD.
@larshp
Copy link
Member

larshp commented Sep 23, 2022

yea, its a bug in the abaplint downport, abap on 702 does not have any mechanism for scoping, so the code must rename the variables in the downport

@larshp larshp mentioned this issue Sep 24, 2022
@pokrakam
Copy link
Collaborator

It sortof makes sense because it shouldn't work in ABAP. But ABAP cheats and pretends to do local scope when in fact both s's from your first example are the same procedure-local variable. Technically it's declared in the first reduce, reused in the next and hidden from the rest of the procedure body. To verify this, if you try to declare DATA s TYPE string you should get an error.

@larshp
Copy link
Member

larshp commented Sep 24, 2022

hmm, perhaps I can do a fix, hmm, but I also have actual work to do 😅

hmm, no, need to get variable renaming implemented first

@larshp
Copy link
Member

larshp commented Nov 10, 2022

@ceedee666 try again

image

@larshp
Copy link
Member

larshp commented Nov 15, 2022

closing, no reply

@larshp larshp closed this as completed Nov 15, 2022
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

4 participants