Here are a few standard functions that will facilitate your work when dealing with internal tables.

The following internal table will be used for examples.



types: begin of ts_itab,
         order   type i,
         name    type string,
         surname type string,
       end of ts_itab.
  
data : ls_itab type ts_itab,
       lt_itab type standard table of ts_itab.
 
ls_itab-order = 1.
ls_itab-name  = 'John'.
ls_itab-surname  = 'Smith'.
append ls_itab to lt_itab.
clear ls_itab.

ls_itab-order = 2.
ls_itab-name  = 'Emma'.
ls_itab-surname  = 'Johnson'.
append ls_itab to lt_itab.
clear ls_itab.

ls_itab-order = 3.
ls_itab-name  = 'Michael'.
ls_itab-surname  = 'Brown'.
append ls_itab to lt_itab.
clear ls_itab.
  
ls_itab-order = 4.
ls_itab-name  = 'Olivia'.
ls_itab-surname  = 'Davis'.
append ls_itab to lt_itab.
clear ls_itab.

lines( )

The lines( ) function returns the number of rows in the internal table you have provided.


data(lv_line_count) = lines( lt_itab ).
write : lv_line_count.
"4

line_exists( )

The line_exists( ) function checks if the searched values exist in the table. It is used together with an IF statement. If the searched row is in the table, the IF block is executed; otherwise, the ELSE block is executed.


if line_exists( lt_itab[ order = 1                         
                         name  = 'John' ] ).
  "found
else.
  "not found
endif.

line_index( )

The line_index( ) function returns the position of the searched row within the internal table.


data(lv_index) = line_index( lt_itab[ order = 2
                                      name  = 'Emma' ] ).
write : lv_index.
"2