Skip to Content
2
Dec 11, 2017 at 05:40 PM

Strange behaviour of substring in reduce expression with cond

299 Views Last edit Dec 11, 2017 at 05:49 PM 9 rev

Today I stumbled about something which didn't make any sense at all to me. Consider this sample data which I want to reduce to a string omiting the first 5 characters and if there are less than 5 characters I'd insert a line break.

" sample data
DATA(text_tab) = VALUE stringtab( ( `-----Test` )
                                  ( ` ` )
                                  ( `-----Test` )
                                  ( `-----Test` ) ).

The desired result would be `Test\nTestTest`.

Let's try to solve this with reduce expressions. This one gives me the desired result.

DATA(string) = REDUCE string( INIT result = ||
                              FOR line IN text_tab
                              NEXT result = result && 
                                   COND #( WHEN strlen( line ) < 5 THEN |\n|
                                           ELSE substring( val = line
                                                           off = 5 ) ) ).

cl_demo_output=>display( string ).

Now this slightly changed expression does something strange.

DATA(string) = REDUCE string( INIT result = ||
                              FOR line IN text_tab
                              NEXT result = result && 
                                   COND #( WHEN strlen( line ) < 5 
                                           THEN cl_abap_char_utilities=>newline
                                           ELSE substring( val = line
                                                           off = 5 ) ) ).
cl_demo_output=>display( string ).

The only difference between the two examples it the use of string template line break |\n| compared to cl_abap_char_utilities=>newline.

It becomes even stranger.

If I use cl_abap_char_utilities=>cr_lf I get yet another result.

DATA(string) = REDUCE string( INIT result = ||
                              FOR line IN text_tab
                              NEXT result = result && 
                                   COND #( WHEN strlen( line ) < 5 
                                           THEN cl_abap_char_utilities=>cr_lf
                                           ELSE substring( val = line
                                                           off = 5 ) ) ).

cl_demo_output=>display( string ).

That doesn't seem right to me and I can't explain. Am I missing the obvious or is there something rotten in the state of denmark?

Maybe Horst Keller is around?

Regards Christian.

P.S. I'm on S/4 Hana with this kernel.