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 Comprehensions 

Table Comprehensions, Multiple FOR Expressions

This example demonstrates a sequence of FOR expressions in a table comprehension.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    TYPES:
      BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line,
      BEGIN OF line1,
        col1 TYPE i,
        col2 TYPE TABLE OF line WITH EMPTY KEY,
      END OF line1,
      itab1 TYPE TABLE OF line1 WITH EMPTY KEY,
      BEGIN OF line2,
        col1 TYPE i,
        col2 TYPE i,
        col3 TYPE i,
      END OF line2,
      itab2 TYPE TABLE OF line2 WITH EMPTY KEY.

    DATA(out) = cl_demo_output=>new( ).

    DATA(itab1) = VALUE itab1(
      ( col1 = 1 col2 = VALUE line1-col2( ( col1 = 111 col2 = 112 )
                                         ( col1 = 121 col2 = 122 ) ) )
      ( col1 = 2 col2 = VALUE line1-col2( ( col1 = 211 col2 = 212 )
                                         ( col1 = 221 col2 = 222 ) ) )
      ( col1 = 3 col2 = VALUE line1-col2( ( col1 = 311 col2 = 312 )
                                         ( col1 = 321 col2 = 322 ) ) )
                             ).
    LOOP AT itab1 INTO DATA(line1).
      out->write( name = |ITAB1, Line { sy-tabix }, COL2|
                  data = line1-col2 ).
    ENDLOOP.

    DATA(itab2) = VALUE itab2(
      FOR wa1 IN itab1
      FOR wa2 IN wa1-col2
        ( col1 = wa1-col1
          col2 = wa2-col1
          col3 = wa2-col2 ) ).
    out->write( itab2 ).

    out->display( ).

Description

The nested internal table is evaluated in a further FOR expression after the FOR expression for itabl. The result is an internal table, itab2, that contains the content of itab1 in a flat row structure. The behavior of the sequence of FOR expressions is the same as in the nested LOOPs.