Using table comprehensions and reductions

I recently came across to useful features in the new ABAP syntax. These two functions relay on internal tables, where you can do some quick operations on it.

Table comprehension

The first example loads initially a table or a view to the variable lt_connections. The lt_connections contain later the fields, which are in the work area variable line. Every entry in the table will now go through the mapping process to match with our new internal table lt_results, which has slightly different fields.

TYPES: BEGIN OF ts_small_connection,
         carrier      TYPE zcarr_id,
         connection   TYPE zconn_id,
         seats        TYPE i,
         oneweekafter TYPE d,
       END OF ts_small_connection.

DATA lt_results TYPE TABLE OF ts_small_connection.
lt_results = VALUE #( FOR line IN lt_connections
    (   carrier = line-airline
        connection = line-connection
        seats = line-totalseats
        oneweekafter = line-currentflight + 7
    )
).

out->write( lt_results ).

You may know it already from the CORRESPONDING command but with this feature, we can additionally add custom logic to each field. Like in the last field OneWeekAfter, we add 7 days to the current flight date.

Table reductions

The second new command is all about summarizing an internal table. We are using here the same table lt_connections, were we have some rows stored.

What we want to do is to simply sum up the total seats in our table into a new variable called lv_sum. This function will go again through all entries and runs your custom operation.

DATA lv_sum type i.
lv_sum = REDUCE #(
    INIT total = 0
    FOR line in lt_connections
    NEXT total += line-totalseats
).

out->write( lv_sum ).

Result

The top table is showing the result of the table comprehension.
The bottom line shows the total of the reduce calculation.

I hope that you may use these cool new functions instead of using the LOOP AT command.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ad blocker detected

We have detected that you are using extensions to block ads. Please support us by disabling your ad blocker.

Powered By
100% Free SEO Tools - Tool Kits PRO