ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Internal Tables → Expressions and Functions for Internal Tables
FOR - Table Iterations
Other versions:
7.31 | 7.40 | 7.54
Syntax Forms
Read rows of internal tables
1. ... FOR wa|<fs> IN itab [INDEX INTO idx] [
cond] [let_exp] ...
Group rows of internal tables
2. ... FOR GROUPS [group|<group>] OF wa|<fs> IN itab
[INDEX INTO idx] [cond]
GROUP BY group_key
[ASCENDING|DESCENDING [AS TEXT]]
[WITHOUT MEMBERS]
[let_exp] ...
Read group members of internal tables
3. ... FOR { wa|<fs> IN GROUP group [INDEX INTO idx] [
WHERE ( log_exp )] }
| { GROUPS OF
wa|<fs> IN GROUP group [INDEX INTO idx] [
WHERE ( log_exp )]
GROUP BY group_key
[ASCENDING|DESCENDING [AS TEXT]]
[WITHOUT MEMBERS] } [
let_exp] ...
Effect
These syntax forms of an
iteration expression using FOR
perform
table iterations.
- When used in a constructor expression with the reduction operator
REDUCE
, these forms are known as table reductions.
- When used in a constructor expression with the instance operator
NEW
or with the value operatorVALUE
for internal tables, these forms are known as table comprehensions.
A FOR
expression like this evaluates the content of an internal table and
its result can be used to construct the result of the wrapper constructor expression. The three variants
of a FOR
expression for internal tables work in the same way as the following
variants of the statement LOOP AT itab
:
- The first variant
FOR ... IN itab
works in the same way as the regular row variantLOOP AT itab
without the additionGROUP BY
.
- The second variant FOR GROUPS ...
OF works in the same way as the variant for grouping rows with the addition
GROUP BY
.
- The third variant
FOR ... IN GROUP
works in the same way as the variantLOOP AT GROUP
for member loops.
The variables or field symbols declared in the FOR
expressions are local
in these expressions. The local data from all outer FOR
expressions can be
used when their values are defined. As an option, LET
expressions
let_exp
can be used to define local helper fields at the end of each FOR
expression.
The system field sy-tabix
is not set by a FOR
expression. The addition INDEX INTO
can be used instead.
Note
Multiple sequential FOR
expressions with different variants (including the
conditional iteration) can be specified in a constructor expression. These expressions then work in the same way as nested loops.
Example
Generates an internal table jtab
from an internal table itab
using a
table comprehension. Generates a text string str
from the internal table jtab
using a
table reduction. The result in str
is the character string 1, 9, 25.
TYPES itab TYPE TABLE OF i WITH EMPTY KEY.
DATA(itab) = VALUE itab( ( 1 ) ( 3 ) ( 5 ) ).
DATA(jtab) = VALUE itab( FOR wa IN itab ( ipow( base = wa exp = 2 ) ) ).
DATA(str) = REDUCE string( INIT s = ``
FOR wa IN jtab
NEXT s = COND #( WHEN s = `` THEN |{ wa }|
ELSE |{ s }, { wa }| ) ).