about "loop at itab assigning <wa>"
how can i use "loop at itab assigning <wa>" on a internal table with header line .thanks!
sameena attarwala replied
Using a Field Symbol
To assign the contents of the current loop line to a field symbol, specify <result> as follows:
LOOP AT <itab> ASSIGNING <FS> <conditions>.
In each loop pass, the field symbol <FS> points to the table entry read in that pass. If the line type is structured, you should specify the same type for the field symbol when you declare it. This allows you to address the components of the field symbol. If you cannot specify the type statically, you must use further field symbols and the technique of assigning components of structures to address the components of the structure.
The end of the loop does not affect the field symbol, that is, after ENDLOOP it is still assigned to the same line as in the final loop pass. If no table entries are processed in the loop, because the table is entry, or no line meets the condition <condition>, the field symbol is not changed.
DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.
DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY COL1.
FIELD-SYMBOLS <FS> LIKE LINE OF ITAB.
DO 4 TIMES.
LINE-COL1 = SY-INDEX.
LINE-COL2 = SY-INDEX ** 2.
APPEND LINE TO ITAB.
READ TABLE ITAB WITH TABLE KEY COL1 = 2 ASSIGNING <FS>.
<FS>-COL2 = 100.
READ TABLE ITAB WITH TABLE KEY COL1 = 3 ASSIGNING <FS>.
DELETE ITAB INDEX 3.
IF <FS> IS ASSIGNED.
WRITE '<FS> is assigned!'.
LOOP AT ITAB ASSIGNING <FS>.
WRITE: / <FS>-COL1, <FS>-COL2.
The output is:
The example fills a sorted table ITAB with 4 lines. The second line is assigned to the field symbol <FS> (which has the same type), and modified using it. The third line is assigned to <FS> and then deleted. Consequently, the logical expression in the IF statement is untrue. <FS> is used to display the table lines in the LOOP. Afterwards, it points to the third line of the table.