Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Internal Tables →  Expressions and Functions for Internal Tables →  FOR - Table Iterations →  Examples of Table Reductions 

Table Reductions, Summation of an Array

This example demonstrates a simple table reduction using REDUCE.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA itab TYPE TABLE OF i WITH EMPTY KEY.
    itab = VALUE #( FOR j = 1 WHILE j <= 10 ( j ) ).
    cl_demo_output=>write( itab ).

    DATA(sum) = REDUCE i( INIT x = 0 FOR wa IN itab NEXT x = x + wa ).
    cl_demo_output=>write( sum ).

    cl_demo_output=>display( ).

Description

After the FOR expression, the content of the table rows in itab is added to the local variable x and this variable is provided as the result. Instead of x = 0, x TYPE i could also be specified after INIT.