Sunday, 13 April 2025

Using the “FOR” Iteration Expression:

 In SAP ABAP, the FOR-iteration expression is used for loop constructions. It allows you to iterate over a range of values or a table and perform operations within the loop. The syntax and usage of the FOR expression can vary depending on the context.


Before:

APPEND LINES OF lt_itab1 TO l t_itab2.

lt_itab2[] = lt_itab1[].

After:

FOR wa| IN itab [INDEX INTO idx] [cond]

Before :

TYPES: BEGIN OF ty_ship,

        tknum TYPE tknum,

        name TYPE ernam,

        city TYPE ort01,

        route TYPE route,

        END OF ty_ship.


TYPES: ty_ships TYPE SORTED TABLE OF ty_ship WITH UNIQUE KEY tknum.

TYPES: ty_citys TYPE STANDARD TABLE OF ort01 WITH EMPTY KEY.

DATA: gt_citys TYPE ty_citys,

            gs_ship TYPE ty_ship,

            gs_city TYPE ort01.

LOOP AT gt_ships INTO gs_ship.

    gs_city = gs_ship-city.

    APPEND gs_city TO gt_citys.

ENDLOOP.

After:

DATA(gt_citys) = VALUE ty_citys( FOR ls_ship IN gt_ships ( ls_ship-city ) ).

Before with conditions:

DATA: gt_citys TYPE ty_citys,

            gs_ship TYPE ty_ship,

            gs_city TYPE ort01.


LOOP AT gt_ships INTO gs_ship WHERE route = 'R0001'.

                    gs_city = gs_ship-city.

                APPEND gs_city TO gt_citys.

ENDLOOP.

After with conditions:

DATA(gt_citys) = VALUE ty_citys( FOR ls_ship IN gt_ships

WHERE ( route = 'R0001' ) ( ls_ship-city ) ).

No comments:

Post a Comment