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
.
- The
#
character as a symbol for the operand type.
- 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 afterINIT
create local variablesx1
,x2
, ... or field symbols<x1>
,<x2>
, ... to which the same applies with respect to namespace and visibility as to the helper fields declared in aLET
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 usingTYPE
.
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 typetype
.
- All other variables or field symbols declared after
INIT
are optional helper fields that can be on the left side of assignments afterNEXT
(unlike those declared afterLET
).
- At least one iteration
expression must then be specified using
FOR
and it is also possible to specify multiple consecutive iteration expressions.
- If conditional iterations are used, the result is created in custom iteration steps.
- 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 lastFOR
expression. A value or memory area must be assigned to all variables and field symbols declared afterINIT
. 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 variablesx
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 forrhs
.
- 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 usingASSIGN
, which means that only the expressions specified there can be used.
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 theFOR
expressions. This enables the content of the rows of the internal tables to be accessed and the result constructed.
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:
- Conditional iterations are generally used to reduce sets of data objects to a single data object.
- 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.
- Table filtering can be implemented more efficiently using the
filter operator
FILTER
.
- When a local field symbol
<xi>
is used on the left side of assignments afterNEXT
, it should be noted that the assignments are not made to the value referenced by the field symbol (as inINIT
andLET
). Instead, the field symbol is set in the same way as in the statementASSIGN
.
- 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 theFOR
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 typetype
of theREDUCE
expression is used for this character if possible.
- Table reductions are also possible with
FOR
expressions for mesh paths.
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 ) ).