Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Program Flow Logic →  Iteration Expressions →  FOR - Iteration Expressions →  Examples of Iteration Expressions 

Creating a Matrix Using FOR and VALUE

This example demonstrates how a matrix is created using iterations.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    initialize( ).

    "Old way
    DATA: column     TYPE t_column,
          matrix_old TYPE t_rows.
    DO columns TIMES.
      DATA(idx) = sy-index - 1.
      CLEAR column.
      DO rows TIMES.
        APPEND sy-abcde+idx(1) && |{ sy-index }| TO column.
      ENDDO.
      APPEND column TO matrix_old.
    ENDDO.

    "New way
    DATA(matrix_new) =
      VALUE t_rows(
        FOR i = 0 UNTIL i > columns - 1 (
          VALUE t_column(
            FOR j = 1 UNTIL j > rows
              ( sy-abcde+i(1) && |{ j }| ) ) ) ).

    ASSERT matrix_new = matrix_old.

    TRY.
        cl_demo_output=>display( matrix_new[ x ][ y ] ).
      CATCH cx_sy_itab_line_not_found.
        cl_demo_output=>display( 'Not found' ).
    ENDTRY.

Description

The rows and columns of a matrix are simulated using an internal table whose row types are arrays. These tables are filled using nested DO loops and equivalent iterations expressions for conditional iterations in a constructor expression with the operator VALUE. The ASSERT statement shows that both internal tables have the same content. It is possible to access each individual element.