Skip to Content
4
Nov 24, 2017 at 11:09 AM

ABAP: Unexpected behaviour of reduce expression

225 Views Last edit Nov 24, 2017 at 11:11 AM 2 rev

Today I stumbled across an unexpected behaviour in a reduce table expression and couldn't find any hint in the documentation.

Consider these two reduce operations.

    TYPES: BEGIN OF ty_output,
             head     TYPE string,
             position TYPE string,
           END OF ty_output.


    SELECT FROM t100
           FIELDS *
           WHERE sprsl = @sy-langu
           ORDER BY PRIMARY KEY
           INTO TABLE @DATA(t100_tab)
           UP TO 10 ROWS.


    DATA(output) = REDUCE ty_output( INIT result = VALUE ty_output( )
                                     FOR GROUPS group OF ls_t100 IN t100_tab
                                     INDEX INTO index
                                     GROUP BY ( arbgb = ls_t100-arbgb
                                                msgnr = ls_t100-msgnr )
                                     FOR <ls_t100> IN GROUP group
                                     NEXT result = VALUE #( position = result-position && |\n{ <ls_t100>-text }| ) ). " <=== difference here


    DATA(output2) = REDUCE ty_output( INIT result = VALUE ty_output( )
                                      FOR GROUPS group OF ls_t100 IN t100_tab
                                      INDEX INTO index
                                      GROUP BY ( arbgb = ls_t100-arbgb
                                                 msgnr = ls_t100-msgnr )
                                      FOR <ls_t100> IN GROUP group
                                      NEXT result-position = result-position && |\n{ <ls_t100>-text }| ). " <=== difference here


    " fails:
*    ASSERT output = output2.

    cl_demo_output=>write( output-position ).
    cl_demo_output=>write( output2-position ).
    cl_demo_output=>display( ).

I expected that both return the same value. They don't.

Can anybody explain that? Is that documented somewhere?

Maybe Horst Keller is around ;-)

Thanks.

Christian

P.s. You can also find the code in this abapGit repo.

Attachments