ABAP - Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Internal Tables →  Expressions and Functions for Internal Tables → 

FOR - Table Iterations

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.

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 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 }| ) ).



Continue
FOR ... IN itab
FOR GROUPS ... OF
FOR ... IN GROUP
FOR - cond
Examples of Table Reductions
Examples of Table Comprehensions
Examples of Grouping with FOR