You have already learned how to iterate over an internal table using a structure as a work area. When you use this technique, the system copies data from the internal table into the work area. You then change the contents of the work area before copying them back into the internal table using the
If you use this technique to process a large internal table, you risk causing performance problems because of the costs of copying the data back and forth. You can reduce these costs by replacing the work area with a field symbol.
Replacing a Work Area With a Field Symbol
A field symbol is a pointer. A pointer is a data object that knows the memory address of a different object and allows you to manipulate that object. In the case of internal tables, the pointer allows you to address a line of an internal table without first copying it into a work area. Since you work directly with the table line and not with a work area, you do not have to copy your changes back into the internal table.
To define a field symbol, you use the
FIELD-SYMBOLS statement and assign a name to the field symbol. The name must be included in angled brackets. You also assign a type to the field symbol. If you want to use a field symbol to process an internal table, you define the field symbol with the line type of the internal table, just as you would with a work area.
To use the field symbol in a loop over an internal table, you use the
ASSIGNING addition in the
LOOP statement. In each loop pass, the pointer now points to the corresponding line of the internal table and you can change its contents directly. As the field symbol has the line type of the internal table, you can treat it as you would a corresponding structure. After the name of the field symbol, you can type a dash, followed by the field that you want to change.
Since you are working directly with the internal table and not with a structure, there is no need for the
MODIFY statement. By avoiding copying data, you can improve the performance of the loop considerably.
Here you can see the result of a runtime analysis in which an internal table is updated using a
MODIFY statement and another is updated using a field symbol. In this case, the field symbol takes 40% less time than the modify statement. As a rule, you can expect to save 25–40% of the runtime when you update an internal table using a field symbol instead of a work area. The performance gain for read operations is smaller.