Situation
Sometimes you loop through an internal table and you need to reference the previous item. This can easily be done by creating a variable of the same type as the table lines and filling it at the end of every loop. But this is not handy if you also want to make changes to this previous line. You could introduce an index variable and update the table using this index but there is a simpler way.
Using a reference variable you can point back to any previous entry in the table as long as you assign it when you are looping at the entry. The advantages are that you can also still change this entry and it is memory efficient.
Example
DATA: data TYPE STANDARD TABLE OF i,
prev TYPE REF TO i.
FIELD-SYMBOLS: <i> TYPE i.
START-OF-SELECTION.
" Fill table with 1 .. 10
DO 10 TIMES.
APPEND sy-index TO data.
ENDDO.
" Process all items and write out current and previous values
LOOP AT data ASSIGNING <i>.
WRITE: / <i>.
IF prev IS NOT INITIAL.
" PREV has been assigned, write it out,
" without this check you will get a NULL exception
WRITE: prev->*.
ENDIF.
" Save reference to this entry as previous
GET REFERENCE OF <i> INTO prev.
ENDLOOP.