Search code examples
referenceabapinternal-tables

How to retrieve the position of an internal table line from a reference to that line?


Is there a way to retrieve the relative position of a line in an internal table from a reference to that line ? The table line is not unique.

I need to keep track of the positions of the lines of a table after the table is sorted, or if lines are inserted in the middle.


Solution

  • You CANNOT DECENTLY retrieve the position of an internal table line from a reference to that line. A reference just points to the memory, it doesn't know the internal table management information.

    By "cannot decently", I mean that it's not decent to use the workaround by looping at all the lines of the internal table (slow) and compare their references till you find the right line.

    A reference to a line always points to the same line after a table is sorted and modified. I can't confirm it's "guaranteed" because it's not clearly stated in the ABAP documentation, but no doubt for me that it is because the logical order is stored in a separate index, as explained in itab - Line-Based Administration Costs:

    "[...] additional memory is required for administrating each individual line, so allowing optimized access to the individual lines. This internal administration of individual lines has two variants:"

    • "A table index administrates the logical order of table lines. The additional memory requirement is 6 bytes per table line on average. Exception: If the logical order matches the physical order in the table body, no additional memory is required for the index."
    • "Hash administration [...]"

    An example confirms what happens after SORT.

    Also, the Data References chapter says that they can point to lines of internal tables without mention of restrictions concerning SORT or whatever:

    "Data references can point to any data objects or to their parts (components, lines of internal tables, or subareas determined by offset or length specifications)."