Entering content frameChanging Table Lines Using the Index Locate the document in its SAP Library structure

You can use the MODIFY statement to change lines in tables using their index. There is also a special variant of the WRITE TO statement that you can use to modify standard tables.

Changing Single Lines with MODIFY

To change a line using its index, use the following statement:

MODIFY <itab> FROM <wa> [INDEX <idx>] [TRANSPORTING <f1> <f 2> ... ].

The work area <wa> specified in the FROM addition replaces the existing line in <itab>. The work area must be convertible into the line type of the internal table.

If you use the INDEX option, the contents of the work area overwrites the contents of the line with index <idx>. If the operation is successful, SY-SUBRC is set to 0. If the internal table contains fewer lines than <idx>, no line is changed and SY-SUBRC is set to 4.

Without the INDEX addition, you can only use the above statement within a LOOP. In this case, you change the current loop line <idx> is implicitly set to SY-TABIX.

When you change lines in sorted tables, remember that you must not change the contents of key fields, and that a runtime error occurs if you try to replace the contents of a key field with another value. However, you can assign the same value.

The TRANSPORTING addition allows you to specify the fields that you want to change explicitly in a list. See also Changing Table Entries. If you change a sorted table, you may only specify non-key fields.

Changing Lines Using WRITE TO

You can change lines of standard tables using the following statement:

WRITE <f> TO <itab> INDEX <idx>.

This variant of the WRITE TO statement converts the contents of field <f> to type C and then transfers the resulting character string into the line with index <idx>. If the operation is successful, SY-SUBRC is set to 0. If the internal table contains fewer lines than <idx>, no line is changed and SY-SUBRC is set to 4.

The data type of <f> must be convertible into a character field; if it is not, a syntax or runtime error occurs. The line is always interpreted as a character string, regardless of its actual line type. You can process components in the same way as in the normal WRITE TO statement. You should only use this statement for structured line types if you want to change a single character whose exact position you already know. Another possibility is to use internal tables whose structure is made up of a single character field. Tables of this kind are often used in dynamic programming.

Examples

Example

DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.

DATA ITAB LIKE TABLE OF LINE.

DO 3 TIMES.
  LINE-COL1 = SY-INDEX.
  LINE-COL2 = SY-INDEX ** 2.
  APPEND LINE TO ITAB.
ENDDO.

LOOP AT ITAB INTO LINE.
  IF SY-TABIX = 2.
     LINE-COL1 = SY-TABIX * 10.
     LINE-COL2 = ( SY-TABIX * 10 ) ** 2.
     MODIFY ITAB FROM LINE.
  ENDIF.
ENDLOOP.

LOOP AT ITAB INTO LINE.
  WRITE: / SY-TABIX, LINE-COL1, LINE-COL2.
ENDLOOP.

This produces the following output:

         1         1          1
         2        20        400
         3         3          9

Here, a sorted table ITAB is created and filled with three lines. The second line is replaced by the contents of the work area LINE.

Example

DATA NAME(4) VALUE 'COL2'.

DATA: BEGIN OF LINE,
         COL1 TYPE I,
         COL2 TYPE I,
      END OF LINE.

DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY COL1.

DO 4 TIMES.
  LINE-COL1 = SY-INDEX.
  LINE-COL2 = SY-INDEX ** 2.
  APPEND LINE TO ITAB.
ENDDO.

LINE-COL2 = 222.
MODIFY ITAB FROM LINE INDEX 2 TRANSPORTING (NAME).

LINE-COL1 = 3.
LINE-COL2 = 333.
MODIFY ITAB FROM LINE INDEX 3.

LOOP AT ITAB INTO LINE.
  WRITE: / SY-TABIX, LINE-COL1, LINE-COL2.
ENDLOOP.

The output is:

         1         1          1
         2         2        222
         3         3        333
         4         4         16

The example fills a sorted table with four lines. In the second and third lines, the component COL2 is modified. If the third line were to be changed so that the value of LINE-COL1 was no longer 3, a runtime error would occur, since the key fields of sorted tables may not be changed.

Example

DATA TEXT(72).

DATA CODE LIKE TABLE OF TEXT.

TEXT = 'This is the first line.'.

APPEND TEXT TO CODE.

TEXT = 'This is the second line. It is ugly.'.

APPEND TEXT TO CODE.

TEXT = 'This is the third and final line.'.

APPEND TEXT TO CODE.

WRITE 'nice.' TO CODE+31 INDEX 2.

LOOP AT CODE INTO TEXT.

   WRITE / TEXT.

ENDLOOP.

This produces the following output:

This is the first line.
This is the second line. It is nice.
This is the third and final line.

Here, an internal table CODE is defined with an elementary type C field which is 72 characters long. After filling the table with three lines, the second line is changed by using the WRITE TO statement. The word "ugly" is replaced by the word "nice".

 

 

 

 

Leaving content frame