I have an ABAP internal table. Structured, with several columns (e.g. 25). Names and types are irrelevant. The table can get pretty large (e.g. 5,000 records).
| A | B | ... |
| --- | --- | --- |
| 7 | X | ... |
| 2 | CCC | ... |
| 42 | DD | ... |
Now I'd like to set one of the columns (e.g. B) to a specific constant value (e.g. 'Z').
What is the shortest, fastest, and most memory-efficient way to do this?
My best guess is a LOOP REFERENCE INTO
. This is pretty efficient as it changes the table in-place, without wasting new memory. But it takes up three statements, which makes me wonder whether it's possible to get shorter:
LOOP AT lt_table REFERENCE INTO DATA(ls_row).
ls_row->b = 'Z'.
ENDLOOP.
Then there is the VALUE
operator which reduces this to one statement but is not very efficient because it creates new memory areas. It also gets longish for a large number of columns, because they have to be listed one by one:
lt_table = VALUE #( FOR ls_row in lt_table ( a = ls_row-a
b = 'Z' ) ).
Are there better ways?
REFERENCE INTO
vs.ASSIGNING
? Other than that, this is probably the fastest method. For theVALUE
expression, you might be able to useCORRESPONDING ... MAPPING ...
but since I don't have any performance measurements (and am not on a hunt for points), that's not enough for an answer... – WakeupREFERENCE INTO
andASSIGNING
turned out equally fast. Though the one required reference seems to use a tiny bit more memory - on a scale that's irrelevant to me.CORRESPONDING
doesn't work because it only supports moving columns from one table to the next, but not setting them to fixed values. – Haddington