ABAP: Get index of a row from an internal table with table expressions

With ABAP 7.40 you can use table expression to quickly get an index of a row from an internal table. You can use that to quickly check if a row is present an then change that row without searching a second time.

DATA(lt_matnr) = VALUE mara_tt( ( matnr = '4711' ) ).

DATA(lv_index) = line_index( lt_matnr[ matnr = '4711' ] ).
IF lv_index IS NOT INITIAL.
  lt_matnr[ lv_index ]-matnr = '4712'.
ENDIF.
1
2
3
4
5
6

Or if you prefer a try/catch syntax:

DATA(lt_matnr) = VALUE mara_tt( ( matnr = '4711' ) ).

TRY.
    lt_matnr[ line_index( lt_matnr[ matnr = '4711' ] ) ]-matnr = '4712'.
  CATCH cx_sy_itab_line_not_found.
ENDTRY.
1
2
3
4
5
6