Backward Loop of Internal Table

By default- when we have an internal table with few set of records and we loop over that then the loop goes in a forward direction over the set of records.

Now what if you want to loop over the records from last to first, one obvious way is to sort the records so that the last records becomes first and the first records becomes last.

But without SORTing the internal table records, use the keyword: STEP and provide the sign as -(negative) which is the backward direction and the number specifies how many record(s) we want to iterate over.

Below here a simple example- that goes over forward direction which is the default and the other loop with STEP -1 that goes over the backward direction.

Output-


2 comments

  1. REPORT zsub_rp_loopback.

    SELECT * FROM zsub_dt_semp INTO TABLE @DATA(lt_scarr) UP TO 5 ROWS.

    IF lt_scarr IS NOT INITIAL.

      WRITE: / ‘ Forward looping’.
      LOOP AT lt_scarr ASSIGNING FIELD-SYMBOL(<fs_forward>).
        WRITE: / <fs_forward>-id.
      ENDLOOP.
      ULINE.

      WRITE: / ‘ Backward loop’.
      LOOP AT lt_scarr ASSIGNING FIELD-SYMBOL(<fs_backward>) STEP -1. 
        WRITE: / <fs_backward>-id.
      ENDLOOP.
    ENDIF.

    I am trying to achieve backward loop but i am getting error,

    I am mention error below.

    “.”, “TO”, “FROM”, “WHERE”, “WHERE ….”, or “USING KEY …” is expected after “FIELD-SYMBOL()”.

    how to solve this.

    Like

Leave a reply to Anonymous Cancel reply