Working with Simple Internal Tables

Objectives

After completing this lesson, you will be able to:

  • Define simple internal tables
  • Process data using simple internal tables

Definition of Simple Internal Tables

Internal Tables

Internal tables are variable data objects in which you can store several values of identical type. This type has to be specified in the declaration and is called the row type of the internal table.

Each value occupies one row of the internal table. The number of rows is not restricted. Theoretically, you can store any number of values in one internal table. Limitations only come from technical boundaries like available memory or system configuration.

The initial value of an internal table is an empty table or, in other words, a table with 0 lines. There are different techniques for filling an internal table. The example uses the APPEND statement to add a new row at the end of the internal table and fill it with a value.

Table Types

The type of an internal table is called a table type.

In the previous example we used TYPE TABLE OF in the DATA statement directly. The table type was bound to the declared variable.

As an alternative you can use TYPE TABLE OF in a TYPES statement to define a table type with a name. You can then use this table type, for example, in a DATA statement. The visibility of these types depends on the position of the TYPES statement.

There are also table types in the ABAP Dictionary. These table type are maintained with a dedicated editor. They are called global table types because they are visible anywhere in the system.

Data Processing with Simple Internal Tables

Watch this video to know how you can fill an internal table with the APPEND statement.

The initial value of an internal table is an empty table, that is, an internal table with zero rows.

You already learned that with statement CLEAR you can reset an ABAP variable to its type-specific initial value.

When you use CLEAR for an internal table, you delete all its content and set the number of rows to zero.

There are various ways to retrieve data from an internal table. This example retrieves the content of a single row using an internal table expression. In the table expression, the name of the internal table is followed immediately by a pair of square brackets. An integer expression inside the brackets specifies the position of the row to be read.

Note:

Correct syntax requires at least one blank after the opening bracket and before the closing bracket.

Reading from an Internal Table in a Loop

The following video illustrates how you can read from an internal table in a loop.

When you implement a loop over an internal table, you can use an inline declaration after addition INTO instead of declaring the work area explicitly with a DATA statement.

By doing so, you not only reduce the amount of code you have to type, you also ensure that the type of work area fits the line type of the internal table, because the type of the inline declared data object is derived from the context, which, in this case, is the internal table.

Try It Out: Simple Internal Tables

  1. Like in the first exercise of this course, create a new global class that implements interface IF_OO_ADT_CLASSRUN.
  2. Copy the following code snippet to the implementation part of method if_oo_adt_classrun~main( ):
    Code snippet
    
    * Declarations
    **********************************************************************
    
        " Internal tables
        DATA numbers TYPE TABLE OF i.
    
        "Table type (local)
        TYPES tt_strings TYPE TABLE OF string.
        DATA texts1      TYPE tt_strings.
    
        " Table type (global)
        DATA texts2 TYPE string_table.
    
        " work areas
        DATA number TYPE i VALUE 1234.
        DATA text TYPE string.
    
    * Example 1: APPEND
    **********************************************************************
    
        APPEND 4711       TO numbers.
        APPEND number     TO numbers.
        APPEND 2 * number TO numbers.
    
        out->write(  `-----------------` ).
        out->write(  `Example 1: APPEND` ).
        out->write(  `-----------------` ).
    
        out->write( numbers ).
    
    * Example 2: CLEAR
    **********************************************************************
    
        CLEAR numbers.
    
        out->write(  `----------------` ).
        out->write(  `Example 2: CLEAR` ).
        out->write(  `----------------` ).
    
        out->write( numbers ).
    
    * Example 3: table expression
    **********************************************************************
        APPEND 4711       TO numbers.
        APPEND number     TO numbers.
        APPEND 2 * number TO numbers.
    
        out->write(  `---------------------------` ).
        out->write(  `Example 3: Table Expression` ).
        out->write(  `---------------------------` ).
    
        number = numbers[ 2 ] .
    
        out->write( |Content of row 2: { number }|    ).
        "Direct use of expression in string template
        out->write( |Content of row 1: { numbers[ 1 ]  }| ).
    
    * Example 4: LOOP ... ENDLOOP
    **********************************************************************
        out->write(  `---------------------------` ).
        out->write(  `Example 4: LOOP ... ENDLOOP` ).
        out->write(  `---------------------------` ).
    
        LOOP AT numbers INTO number.
    
          out->write( |Row: { sy-tabix } Content { number }| ).
    
        ENDLOOP.
    
    * Example 5: Inline declaration in LOOP ... ENDLOOP
    **********************************************************************
        out->write(  `-----------------------------` ).
        out->write(  `Example 5: Inline Declaration` ).
        out->write(  `-----------------------------` ).
    
        LOOP AT numbers INTO DATA(number_inline).
          out->write( |Row: { sy-tabix } Content { number_inline }| ).
        ENDLOOP.
    Expand
  3. Press CTRL + F3 to activate the class and F9 to execute it as a console app.
  4. Analyze the console output. Play around with the source code to get familiar with the concepts.

Work with Simple Internal Tables

In this exercise, you use iterations and simple internal tables to calculate and display the Fibonacci numbers.

Note

The Fibonacci numbers are a sequence of numbers in which each number is the sum of the two preceding ones. The sequence starts with 0 and 1, and each subsequent number is the sum of the two previous numbers. The sequence has various mathematical properties, patterns, and applications in fields such as mathematics, computer science, nature, and art.

Template:

  • none

Solution:

  • /LRN/CL_S4D400_BTS_ITERATE (global Class)

Task 1: Calculate the Numbers

In your own package, create a new global class. Define a constant for the number of iterations and an internal table for the Fibonacci numbers. Then calculate the Fibonacci numbers in an iteration and store them in the internal table.

Steps

  1. Create a new global class that implements the interface IF_OO_ADT_CLASSRUN (suggested name: ZCL_##_ITERATE, where ## stands for your group number).

    1. Choose FileNewABAP Class.

    2. Enter the name of your package in the Package field. In the Name field, enter the name ZCL_##_ITERATE, where ## stands for your group number.

    3. Enter a description.

    4. In the Interfaces group box, choose Add.

    5. Enter IF_OO_ADT_CLASSRUN. When the interface appears in the hit list, double-click it to add it to the class definition.

    6. Choose Next.

    7. Select your transport request and choose Finish.

  2. In the IF_OO_ADT_CLASSRUN~MAIN method, define a constant for the number of iterations (suggested name: count) with a small value, for example 20.

    1. Add the following code:

      Code snippet
      
         CONSTANTS max_count TYPE i VALUE 20.
      
      Expand
  3. Declare a simple internal table to store the Fibonacci numbers (suggested name: numbers).

    1. Add the following code:

      Code snippet
      
        DATA numbers TYPE TABLE OF i.
      
      Expand
  4. Implement an iteration that is executed max_count times.

    1. After the declarations, add the following code:

      Code snippet
      
        DO max_count TIMES.
      
        ENDDO.
      
      Expand
  5. Inside the iteration, implement a case distinction based on the built-in iteration counter: In the first iteration, add the value 0 to the internal table numbers. In the second iteration, add the value 1 to the end of internal table numbers.

    Hint
    The built-in iteration counter is system field sy-index.
    1. Between DO and ENDDO, add the following code:

      Code snippet
      
         CASE sy-index.
            WHEN 1.
              APPEND 0 TO numbers.
            WHEN 2.
              APPEND 1 TO numbers.
         ENDCASE.
      
      Expand
  6. Add a branch that is executed for all other values. In this branch, calculate the new entry as the sum of the two preceding entries in numbers.

    Hint
    Access the preceding entries using index sy-index - 1 and sy-index - 2.
    1. Add the highlighted code:

      Code snippet
      
         CASE sy-index.
            WHEN 1.
              APPEND 0 TO numbers.
            WHEN 2.
              APPEND 1 TO numbers.
         ENDCASE.
      
      Expand

Task 2: Prepare a Formatted Output

In a loop over the internal table, prepare a formatted output that lists each Fibonacci number with its respective sequential number. Prepare the output in another internal table of row type string.

Steps

  1. At the beginning of the method if_oo_adt_classrun~main, declare an internal table of row type string (suggested name: output).

  2. Implement a loop over the internal table numbers to read the Fibonacci numbers one by one into a variable number.

    Hint
    Use an inline declaration for number.
    1. At the end of the method, add the following code:

      Code snippet
      
        LOOP AT numbers INTO DATA(number).
      
        ENDLOOP.
      
      Expand
  3. Declare an integer variable (suggested name: counter), that you set to zero before the loop and increase by 1 in each iteration.

    Hint
    You can use an inline declaration for counter.
    1. Add the highlighted code:

      Code snippet
      
        LOOP AT numbers INTO DATA(number).
      
        ENDLOOP.
      
      Expand
  4. In each iteration, add a new row to the internal table output which contains the content of counter (l4 characters wide, left-justified), a colon (;) and the content of number (10 characters wide, right justified).

    Hint
    Use a string template with suitable format options to adjust the format.
    1. Add the highlighted code:

      Code snippet
      
        DATA(counter) = 0.
        LOOP AT numbers INTO DATA(number).
      
           counter = counter + 1.
      
        ENDLOOP.
      
      Expand

Task 3: Output and Test

Write the result to the console. Then activate and test your class as a console app.

Steps

  1. At the end of the method, call the method out->write to write the content of output to the console. Supply the importing parameter name with a suitable caption.

    1. After ENDLOOP., add the following code:

      Code snippet
      
      out->write( 
               data   = output
               name   = |The first { max_count } Fibonacci Numbers|
                      ) .
      
      Expand
  2. Activate the class.

    1. Press Ctrl + F3 to activate the class.

  3. Execute your class as a console app.

    1. Press F9 to execute the class as console app.

Log in to track your progress & complete quizzes