ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Internal Tables →  Processing Statements for Internal Tables →  AT - itab → 

Control Level Processing

This example demonstrates the control level processing in internal tables.

Source Code

REPORT demo_int_tables_at_1.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.

    DATA: BEGIN OF line,
             col1 TYPE c LENGTH 1,
             col2 TYPE i,
             col3 TYPE i,
          END OF line.

    DATA itab LIKE HASHED TABLE OF line
              WITH UNIQUE KEY col1 col2.

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

    line-col1 = 'A'.
    DO 3 TIMES.
      line-col2 = sy-index.
      line-col3 = sy-index ** 2.
      INSERT line INTO TABLE itab.
    ENDDO.

    line-col1 = 'B'.
    DO 3 TIMES.
      line-col2 = 2 * sy-index.
      line-col3 = ( 2 * sy-index ) ** 2.
      INSERT line INTO TABLE itab.
    ENDDO.

    SORT itab.

    LOOP AT itab INTO line.
      out->write( line ).
      AT END OF col1.
        SUM.
        out->line( )->write( line ).
      ENDAT.
      AT LAST.
        SUM.
        out->line( )->write( line ).
      ENDAT.
    ENDLOOP.
    out->display( ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

Description

A hashed table itab is created, filled with six rows, and sorted. In the LOOP - ENDLOOP, the work area line is used and is displayed for every loop pass. The first field of the primary table key col1 is used for control level processing. The sum of all numerical fields is calculated each time the contents of col1 changes and once for the last table row.