Appearance
Lesson 2 — Internal Tables, Loops & Methods
ABAP uses internal tables for temporary collections of data in program memory.
text
single value
↓
internal table
↓
LOOP
↓
processingNo database is involved in this lesson.
Lesson information
| Duration | About 120 minutes |
| Main object | ZCL_ABAP_L02_### |
| Prerequisite | Lesson 1 |
Learning objectives
You should be able to:
- declare standard internal tables;
- use
WITH EMPTY KEYdeliberately; - create/populate tables with
VALUE; - append values;
- loop with
LOOP AT; - use inline loop variables;
- understand
sy-tabixfor index-table loops; - use
WHEREwhile looping; - read a line with a table expression;
- use
line_existsappropriately; - use arithmetic and
MODin loops; - declare instance methods;
- use
IMPORTINGandRETURNINGparameters; - call functional methods;
- separate calculation logic into methods.
1. Create the executable class
Create:
text
ZCL_ABAP_L02_###Implement:
text
IF_OO_ADT_CLASSRUNUse the same ADT workflow from Lesson 1.
2. What is an internal table?
An internal table is an in-memory ABAP data object containing zero or more lines of the same line type.
text
Internal table of i
┌──────┐
│ 5 │
│ 10 │
│ 15 │
└──────┘It exists during program execution. It is not a persistent database table.
3. Declare a standard table
abap
DATA numbers TYPE STANDARD TABLE OF i WITH EMPTY KEY.Breakdown:
text
STANDARD TABLE table category
OF i line type
WITH EMPTY KEY no meaningful primary key for this collectionFor a simple ordered list of numbers, an empty key is appropriate.
4. Populate with VALUE
Modern ABAP can create the whole table in one expression:
abap
DATA(numbers) = VALUE STANDARD TABLE OF i(
( 5 )
( 10 )
( 15 )
( 20 )
).When a named table type exists, the syntax becomes even cleaner. Lesson 3 will introduce that.
5. APPEND
You can also add a line procedurally:
abap
APPEND 25 TO numbers.Both approaches are valid.
Use VALUE when constructing a known set of values. Use statements such as APPEND/INSERT when values are being added during processing.
6. Loop through the table
abap
LOOP AT numbers INTO DATA(number).
out->write( number ).
ENDLOOP.number is the current table line.
7. sy-tabix
For standard and sorted table index processing, sy-tabix contains the current table index during a normal loop.
abap
LOOP AT numbers INTO DATA(number).
out->write( |Index { sy-tabix }: { number }| ).
ENDLOOP.Do not treat sy-tabix as a general row identity. It is an index-related system field and behaves differently with hashed access.
8. Sum values
abap
DATA(total) = 0.
LOOP AT numbers INTO DATA(number).
total += number.
ENDLOOP.
out->write( |Total: { total }| ).9. Count values that satisfy a rule
abap
DATA(count) = 0.
LOOP AT numbers INTO DATA(number).
IF number > 10.
count += 1.
ENDIF.
ENDLOOP.10. Even numbers with MOD
MOD gives the remainder of integer division.
abap
IF number MOD 2 = 0.
out->write( |{ number } is even| ).
ENDIF.11. LOOP ... WHERE
Filtering can be expressed directly in the loop:
abap
LOOP AT numbers INTO DATA(number) WHERE table_line > 10.
out->write( number ).
ENDLOOP.For tables with elementary line types, table_line refers to the line value in such conditions.
12. Read by index with a table expression
abap
DATA(second_number) = numbers[ 2 ].Table expressions are concise, but if the requested line does not exist they can raise CX_SY_ITAB_LINE_NOT_FOUND.
If the lookup may legitimately fail, check or use a safe pattern instead of assuming the line exists.
13. Check existence
For a key/index expression whose existence is all you need:
abap
IF line_exists( numbers[ 2 ] ).
out->write( numbers[ 2 ] ).
ENDIF.Avoid checking with line_exists and then repeating an expensive key search unnecessarily in performance-sensitive code. Here the example is small and instructional.
14. Why methods now?
Without methods, main can become a long list of unrelated calculations.
Methods let us write:
text
main
↓ calls
rectangle_area
circle_area
sum_numbersEach method has one clear job.
15. Declare a method
Inside the class public section:
abap
METHODS rectangle_area
IMPORTING
width TYPE decfloat34
height TYPE decfloat34
RETURNING
VALUE(area) TYPE decfloat34.IMPORTING values enter the method.
RETURNING produces one functional return value.
16. Implement the method
abap
METHOD rectangle_area.
area = width * height.
ENDMETHOD.17. Call a functional method
abap
DATA(area) = rectangle_area(
width = CONV decfloat34( '5.5' )
height = CONV decfloat34( '4' )
).
out->write( |Area: { area }| ).The call is an expression because the method has a returning parameter.
18. Add rectangle_perimeter
abap
METHODS rectangle_perimeter
IMPORTING
width TYPE decfloat34
height TYPE decfloat34
RETURNING
VALUE(perimeter) TYPE decfloat34.Implementation:
abap
METHOD rectangle_perimeter.
perimeter = 2 * ( width + height ).
ENDMETHOD.19. Circle calculation
Use a decimal constant locally for the exercise:
abap
DATA(pi) = CONV decfloat34( '3.141592653589793' ).Then:
abap
METHODS circle_area
IMPORTING radius TYPE decfloat34
RETURNING VALUE(area) TYPE decfloat34.Implementation:
abap
METHOD circle_area.
DATA(pi) = CONV decfloat34( '3.141592653589793' ).
area = pi * radius * radius.
ENDMETHOD.Class constants are taught later.
20. Method that accepts an internal table
Define a table type in the class:
abap
TYPES tt_numbers TYPE STANDARD TABLE OF i WITH EMPTY KEY.Method:
abap
METHODS sum_numbers
IMPORTING numbers TYPE tt_numbers
RETURNING VALUE(total) TYPE i.Implementation:
abap
METHOD sum_numbers.
LOOP AT numbers INTO DATA(number).
total += number.
ENDLOOP.
ENDMETHOD.21. Use the method
abap
DATA(numbers) = VALUE tt_numbers(
( 5 ) ( 10 ) ( 15 ) ( 20 )
).
DATA(total) = sum_numbers( numbers = numbers ).
out->write( |Sum: { total }| ).22. Find min and max
A procedural loop is readable for beginners:
abap
DATA(minimum) = numbers[ 1 ].
DATA(maximum) = numbers[ 1 ].
LOOP AT numbers INTO DATA(number).
IF number < minimum.
minimum = number.
ENDIF.
IF number > maximum.
maximum = number.
ENDIF.
ENDLOOP.This assumes the table is not empty. A robust reusable method should define what happens for an empty input.
23. Summary
You now have the core collection and procedure tools used throughout ABAP:
text
internal tables
VALUE
APPEND
LOOP AT
WHERE
sy-tabix
table expressions
line_exists
methods
IMPORTING
RETURNING
functional callsLesson 3 makes internal tables more useful by giving each line a structure and by introducing explicit table keys.