Search This Blog

What is the difference between sy-index and sy-tabix

REPORT ztest.

TYPES: 
BEGIN OF x_emp,
        fname 
TYPE char15,
        lname 
TYPE char15,
       
END OF x_emp.
DATA: t_tab 
TYPE STANDARD TABLE OF x_emp,
      w_tab 
TYPE x_emp.

w_tab-fname = 
'Sam'.
w_tab-lname = 
'Winchester'.
APPEND w_tab TO t_tab.

w_tab-fname = 
'David'.
w_tab-lname = space.

APPEND w_tab TO t_tab.

w_tab-fname = 
'Dean'.
w_tab-lname = 
'Winchester'.
APPEND w_tab TO t_tab.

w_tab-fname = 
'Smith'.
w_tab-lname = space.

APPEND w_tab TO t_tab.

WRITE: / 
'**LOOP Example**'.
LOOP AT t_tab INTO w_tab.
  
IF w_tab-lname IS INITIAL.
    WRITE:/ 
'Index: ', sy-index, 'Tabix: ', sy-tabix.
  ELSE.
    WRITE:/ 
'Index: ', sy-index, 'Tabix: ', sy-tabix,
            
'First Name: ', w_tab-fname, 'Last Name: ', w_tab-lname.
  ENDIF.

ENDLOOP.
SKIP 3.

WRITE: / 
'**DO Example**'.
READ TABLE t_tab INTO w_tab INDEX 2.
DO TIMES.
  WRITE:/ 
'Index: ', sy-index, 'Tabix: ', sy-tabix,
              
'First Name: ', w_tab-fname, 'Last Name: ', w_tab-lname.
ENDDO.

SY-INDEX is used in DO...ENDDO/WHILE loop
SY-TABIX is used in LOOP...ENDLOOP i.e. for tracking line number of internal table
 
SY-TABIX: number of record in the internal table
SY-INDEX: index number of record which is currently executed in the loop...endloop

SY-TABIX

Current line of an internal table. SY-TABIX is set by the statements below, but only for index tables. The field is either not set or is set to 0 for hashed tables.

APPEND sets SY-TABIX to the index of the last line of the table, that is, it contains the overall number of entries in the table.
COLLECT sets SY-TABIX to the index of the existing or inserted line in the table. If the table has the type HASHED TABLE, SY-TABIX is set to 0.
LOOP AT sets SY-TABIX to the index of the current line at the beginning of each loop lass. At the end of the loop, SY-TABIX is reset to the value that it had before entering the loop. It is set to 0 if the table has the type HASHED TABLE.
READ TABLE sets SY-TABIX to the index of the table line read. If you use a binary search, and the system does not find a line, SY-TABIX contains the total number of lines, or one more than the total number of lines. SY-INDEX is undefined if a linear search fails to return an entry.
SEARCH <itab> FOR sets SY-TABIX to the index of the table line in which the search string is found.


SY-INDEX

In a DO or WHILE loop, SY-INDEX contains the number of loop passes (iterations) including the current pass.
Note: sy-index is different from INDEX used with READ statement. In the above example of DO...ENDDO, the record is read using INDEX 2, while you can see different values for sy-index displayed.



 

No comments:

Post a Comment