Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Program Flow Logic →  Iteration Expressions 

REDUCE - Reduction Operator

Other versions: 7.31 | 7.40 | 7.54

Syntax


... REDUCE type( 
      [let_exp]
      INIT {x1 = rhs1}|{<x1> = wrexpr1}|{x1|<x1> TYPE dtype1}
           {x2 = rhs2}|{<x2> = wrexpr2}|{x2|<x2> TYPE dtype2}
           ...
      FOR for_exp1
      FOR for_exp2
      ...
      NEXT ...
           {x1 = rhs1}|{<x1> = wrexpr1}
           {x2 = rhs2}|{<x2> = wrexpr2}
           ... ) ...

Effect

A constructor expression with the reduction operator REDUCE creates a result of a data type specified using type from one or more iteration expressions. The following can be specified for type:

  • A non-generic data type dtype.
  • If the data type required in an operand position is unique and known completely, this type is used.
  • If the operand type is not known completely, the type of the first declaration after INIT (which is always known) is used, except if the constructor expression is passed to an actual parameter with a generically typed formal parameter.
  • If the constructor expression is passed to an actual parameter with a generically typed formal parameter, the operand type is derived in accordance with special rules.

The parameters and arguments of the constructor expression must be specified as follows:

  • Firstly, an optional LET expression can be specified for defining local helper fields whose value is used within the expression but can no longer be modified there.
  • An addition INIT must then be specified followed by any number of declarations. The declarations after INIT create local variables x1, x2, ... or field symbols <x1>, <x2>, ... to which the same applies with respect to namespace and visibility as to the helper fields declared in a LET expression. The syntax of the declaration is one of the following:
  • It is the same as in a LET expression and it follows the rules that apply here.
  • Initial variables or field symbols of the specified data type dtype can be created using TYPE.
At least one variable or one field symbol must be specified. The variables or field symbols declared after INIT can only be used after NEXT. In particular, they cannot be used to declare other variables or field symbols.
  • The first variable x1 or the first field symbol <x1> determines the result of the expression and the data type must be convertible to the result type type.
  • All other variables or field symbols declared after INIT are optional helper fields that can be on the left side of assignments after NEXT (unlike those declared after LET).
  • At least one iteration expression must then be specified using FOR and it is also possible to specify multiple consecutive iteration expressions.
  • If table iterations are used, the rows of existing internal tables are evaluated. This is known as a table reduction, since any values can be constructed from the rows of existing internal tables.
  • Assignments must then be specified after NEXT that are executed for every iteration of the last FOR expression. A value or memory area must be assigned to all variables and field symbols declared after INIT. In the case of structured variables or field symbols, it is enough to make an assignment to one of their components.
  • The assignment on the right side rhs to variables x is made in accordance with the rules of a regular assignment using the assignment operator =, which means that any of the potential calls and expressions can be specified for rhs.
  • Only the result of a writable expression wrexp can be assigned to a field symbol. The same applies here as in assignments of writable expressions using ASSIGN, which means that only the expressions specified there can be used.
The assignments can be specified in any order and they are evaluated from to left to right. Each non-structured variable or field symbol declared after INTIT can be specified precisely once as the left side. In the case of structured variables or field symbols, either the full structure can be specified as the left side or each component once. Alongside the data objects of the program, all local variables and field symbols of the expression visible after the last FOR expression can be used in the operand positions on the right side:
  • All variables and field symbols declared after LET.
  • All variables and field symbols declared after INIT.
  • The wa1, wa2, ... or <fs1>, <fs2>, .... filled by the FOR expressions. This enables the content of the rows of the internal tables to be accessed and the result constructed.
Any helper fields declared after INTIT can only be specified in reading positions on the right side.

After the full evaluation, the content of the first variable x1 or the memory area pointed to by the first field symbol <x1> is assigned to the temporary result of the expression of type type in accordance with the assignment rules.


Notes

  • The name of the operator REDUCE is explained as follows:

  • Table iterations for table reductions are generally used to reduce the content of the internal tables in question to a summary value. The result type can be any type and does not have to be less complex than the internal tables, but this property should only be exploited in exceptional cases. When constructing tables from tables, it is best to use table comprehensions instead.

  • When a local field symbol <xi> is used on the left side of assignments after NEXT, it should be noted that the assignments are not made to the value referenced by the field symbol (as in INIT and LET). Instead, the field symbol is set in the same way as in the statement ASSIGN.

  • Unlike in table comprehensions with the value operator VALUE, an assignment of a REDUCE expression to a structure or an internal table does not overwrite them in full before the FOR expressions are processed. The structure or internal table can be used directly anywhere in the expression.

  • If a variable is declared in the first position after INIT and the VALUE operator is specified with the character # on the right side, the result type type of the REDUCE expression is used for this character if possible.

Example

Calculation of the total of the numbers from 1 to 10 via REDUCE.

cl_demo_output=>display( 
  REDUCE i( INIT s = 0 
            FOR  i = 1 UNTIL i > 10 
            NEXT s = s + i ) ). 

Executable Examples

Continue

REDUCE - Type Inference for Actual Parameter

Reduce Operator, Type Inference