Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

about "loop at itab assigning <wa>"

Former Member
0 Kudos

how can i use "loop at itab assigning <wa>" on a internal table with header line .thanks!

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Mao,

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.

e.g.

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.

ENDDO.

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!'.

ENDIF.

LOOP AT ITAB ASSIGNING <FS>.

WRITE: / <FS>-COL1, <FS>-COL2.

ENDLOOP.

The output is:

1 1

2 100

4 16

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.

Regards,

Sameena

6 REPLIES 6

former_member186741
Active Contributor
0 Kudos

create a field symbol, and use that instead of the header.

eg,

field-symbols <th>.

loop at i_t assigning <th>.

*write:/ <th>-mandt, etc

endloop.

Former Member
0 Kudos

Hi,

A small change, the field symbols will have to be declared like the work area of the table

FIELD-SYMBOLS : <fs_MARA> TYPE MARA

OR

FIELD-SYMBOLS : <fs_MARA> LIKE LINE OF ITAB.

Then Loop at itab assigning <fs_mara>

endloop.

Regards,

Ravi

abdul_hakim
Active Contributor
0 Kudos

hi here is the answer,

data itab like table of mara with header line.

field-symbols <fs> like mara.

loop at itab assigning <fs>.

endloop.

Cheers,

Abdul Hakim

Former Member
0 Kudos

Hi Mao,

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.

e.g.

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.

ENDDO.

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!'.

ENDIF.

LOOP AT ITAB ASSIGNING <FS>.

WRITE: / <FS>-COL1, <FS>-COL2.

ENDLOOP.

The output is:

1 1

2 100

4 16

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.

Regards,

Sameena

0 Kudos

DATA : BEGIN OF GT_ZCO001 OCCURS 0.

INCLUDE STRUCTURE ZCO001.

DATA: END OF GT_ZCO001.

DATA: BEGIN OF GT_FILE OCCURS 0,

ZCNTRY TYPE LAND1,

ZOLOC TYPE ZOLOC,

ZOLOC20 TYPE ZOLOC20,

ZOLOC40 TYPE ZOLOC40,

ZAWSYS TYPE LOGSYSTEM,

END OF GT_FILE.

FIELD-SYMBOLS: <FS_ZCO001> TYPE ZCO001,

<FS_FILE> LIKE GT_FILE.

LOOP AT GT_FILE ASSIGNING <FS_FILE>.

<FS_ZCO001>-ZCNTRY = <FS_FILE>-ZCNTRY.

<FS_ZCO001>-ZOLOC20 = <FS_FILE>-ZOLOC20.

<FS_ZCO001>-ZOLOC40 = <FS_FILE>-ZOLOC40.

<FS_ZCO001>-ZAWSYS = <FS_FILE>-ZAWSYS.

APPEND GT_ZCO001.

CLEAR GT_ZCO001.

ENDLOOP.

Former Member
0 Kudos

Haha, Sameena's answer is what i wanted, but thank you all all the same,experts.