Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

operator REDUCE no proper outcome

0 Kudos
1,091

Dear all.

I'm currently trying out the new operator REDUCE but I have following situation.

I am calling a REST API with the IF_HTTP_CLIENT and CL_REST_HTTP_CLIENT interfaces and wanted to dynamically build the URI query.

    "build query request
    IF iv_search IS SUPPLIED.
      APPEND |search={ iv_search }| TO lt_query.
    ENDIF.

    IF iv_views IS SUPPLIED.
      APPEND |views={ iv_views }| TO lt_query.
    ENDIF.

    IF iv_offset IS SUPPLIED.
      APPEND |offset={ iv_offset }| TO lt_query.
    ENDIF.

    IF iv_limit IS SUPPLIED.
      APPEND |limit={ iv_limit }| TO lt_query.
    ENDIF.

    LOOP AT lt_query ASSIGNING FIELD-SYMBOL(<ls_query>).
      lv_query = |{ lv_query }{ <ls_query> }&|.
    ENDLOOP.

Result:

Currently the code above is working properly and as I always try to learn new Syntax, I tried to replace the LOOP AT with the REDUCE operator.

I followed the F1 Documentation and used the Example and tried following code:

    lv_query = REDUCE #( INIT reduced_query = ''
                         FOR <query> IN lt_query
                         NEXT reduced_query = |{ reduced_query }{ <query> }&| ).

But now the Result looks as follows:

It looks like at some point the definition of one of the defined values is limited to 1 character but I couldn't figure out which one.

The variable lv_query is defined as STRING and the table lt_query as STRING_TABLE.

Do you see any mistake I made with the REDUCE statement?

Thanks for any feedback or help.

BR,

Andreas

1 ACCEPTED SOLUTION

Sandra_Rossi
Active Contributor
736

First of all, it's obvious (from debugger 1 character) that you didn't declare it correctly like this:

DATA lv_query TYPE string.

There's also an issue inside REDUCE, because REDUCED_QUERY is implicitly declared 1 character because of reduced_query = ''. Instead use the reversed quotes to declare it as STRING type:

lv_query = REDUCE #( INIT reduced_query = `` " reversed quotes, or use INIT reduced_query TYPE string
4 REPLIES 4

Sandra_Rossi
Active Contributor
737

First of all, it's obvious (from debugger 1 character) that you didn't declare it correctly like this:

DATA lv_query TYPE string.

There's also an issue inside REDUCE, because REDUCED_QUERY is implicitly declared 1 character because of reduced_query = ''. Instead use the reversed quotes to declare it as STRING type:

lv_query = REDUCE #( INIT reduced_query = `` " reversed quotes, or use INIT reduced_query TYPE string

0 Kudos
736

Oops sorry, I didn't pay attention to the type in the debugger, which is CString{1}. I assumed it was C(1). So please forget about my first "obvious" remark about LV_QUERY type.

736

Hi Sandra,

thanks for you message.

The variable is exactely defined as you mentioned.

But the issue in REDUCE was correct. I changed the single quotes with backquotes and now lv_query is filled properly. I didn't know that you can declare a string literal with backquotes:

Character Literals

I was looking at this documentation and thought that all backquotes were single quotes at first sight.

Table Reductions, String Processing

Thanks for noticing.

BR,

Andreas

0 Kudos
736

Oops, you're right, sorry, I didn't pay attention to the type in the debugger, CString{1}, I assumed it was C(1).