How to manipulate similar fields of structure

Do statement can be used to manipulate similar fields by using it's addition expression "VARYING f FROM f1 NEXT f2", here is a example. Of course,
field-symbol can also do same work, but do statement is more easier.

Notice: If you change the value of f in a do loop, the value of corresponding field is changed until the loop is ended.

REPORT z_varing_test.

DATA: begin of wa_glt0 occurs 0,

hsl01 like glt0-hsl01,

hsl02 like glt0-hsl02,

hsl03 like glt0-hsl03,

hsl04 like glt0-hsl04,

hsl05 like glt0-hsl05,

hsl06 like glt0-hsl06,

hsl07 like glt0-hsl07,

hsl08 like glt0-hsl08,

end of wa_glt0,

l_hsl_total LIKE glt0-hsl01,

l_hsl_tmp LIKE glt0-hsl01.
FIELD-SYMBOLS: <fs> TYPE ANY.

wa_glt0-hsl01 = 1.
wa_glt0-hsl02 = 2.
wa_glt0-hsl03 = 3.
wa_glt0-hsl04 = 4.
wa_glt0-hsl05 = 5.
wa_glt0-hsl06 = 6.
wa_glt0-hsl07 = 7.
wa_glt0-hsl08 = 8.

DO 8 TIMES VARYING l_hsl_tmp FROM wa_glt0-hsl01 NEXT wa_glt0-hsl02.
ADD l_hsl_tmp TO l_hsl_total.
l_hsl_tmp = 2 * l_hsl_tmp.
ASSIGN COMPONENT sy-index OF STRUCTURE wa_glt0 TO <fs>.
WRITE: AT /1 <fs>.
ENDDO.

WRITE: AT /1 'Total:', l_hsl_total.

The result is :

1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 Total: 36.00

Comments are closed.