系统变量SY-INDEX与SY-TABIX是用来记录循环的次数或者索引的,不同的是SY-INDEX在DO....ENDDO中是有效的,而SY-TABIX在LOOP....ENDLOOP中及READ内表时获取相应的索引。
DATA:BEGIN OF STUDENT OCCURS 0,
NAME(10) TYPE C,
AGE TYPE I,
END OF STUDENT.
DATA DENT LIKE LINE OF STUDENT.
STUDENT-NAME = 'JACK'.
STUDENT-AGE = 20.
APPEND STUDENT.
STUDENT-NAME = 'JIM'.
STUDENT-AGE = 25.
APPEND STUDENT.
STUDENT-NAME = 'TOMMY'.
STUDENT-AGE = 28.
APPEND STUDENT.
STUDENT-NAME = 'BUSHI'.
STUDENT-AGE = 20.
APPEND STUDENT.
STUDENT-NAME = 'LILY'.
STUDENT-AGE = 26.
APPEND STUDENT.
STUDENT-NAME = 'ALEX'.
STUDENT-AGE = 28.
APPEND STUDENT.
WRITE: '1.LOOP循环'.
WRITE: / '-----------------------------------------------'.
LOOP AT STUDENT.
WRITE: / 'SY-INDEX=',SY-INDEX LEFT-JUSTIFIED, "LEFT-JUSTIFIED 输出向左对齐
'SY-TABIX=',SY-TABIX LEFT-JUSTIFIED,
'NAME=',STUDENT-NAME LEFT-JUSTIFIED,
'AGE=',STUDENT-AGE LEFT-JUSTIFIED.
ENDLOOP.
WRITE: / '-----------------------------------------------'.
WRITE: / '2.READ INTERNAL TABLE'.
WRITE: / '-----------------------------------------------'.
READ TABLE STUDENT INTO DENT WITH KEY NAME = 'ALEX'. "按具体字段值读取内表
WRITE: / 'SY-INDEX=',SY-INDEX LEFT-JUSTIFIED,
'SY-TABIX=',SY-TABIX LEFT-JUSTIFIED, "读取内表时,返回相应数据在内表中的索引
'NAME=',DENT-NAME LEFT-JUSTIFIED,
'AGE=',DENT-AGE LEFT-JUSTIFIED.
READ TABLE STUDENT INTO DENT WITH KEY NAME = 'BUSHI'. "按具体字段值读取内表
WRITE: / 'SY-INDEX=',SY-INDEX LEFT-JUSTIFIED,
'SY-TABIX=',SY-TABIX LEFT-JUSTIFIED,
'NAME=',DENT-NAME LEFT-JUSTIFIED,
'AGE=',DENT-AGE LEFT-JUSTIFIED.
CLEAR SY-TABIX.
WRITE: / '-----------------------------------------------'.
WRITE: / '3.DO...ENDDO'.
WRITE: / '-----------------------------------------------'.
DO 5 TIMES.
WRITE:/ 'SY-INDEX=',SY-INDEX LEFT-JUSTIFIED,
'SY-TABIX=',SY-TABIX LEFT-JUSTIFIED.
ENDDO.
WRITE: / '-----------------------------------------------'.
从上面的测试例子中,我们不难看出结果。嘎嘎。。