Lets see the various options which are available to do manipulations in new ABAP. The moment we talk about manipulation, first thing comes in our mind is internal table . So we will also start with internal table.
New approach :
TYPES : BEGIN OF ty_int1,
name type char10,
value type char20,
END OF ty_int1.
" Need to specify the type as tt_int1 after VALUE keyword because internal table structure is not defined
( name = 'TECH' value = 'SAP' )
( name = 'DOMAIN' value = 'ABAP' )
).
OR
name type char10,
value type char20,
END OF ty_int1.
DATA : lt_int1 type STANDARD TABLE OF ty_int1 with DEFAULT KEY.
( name = 'TECH' value = 'SAP' )
( name = 'DOMAIN' value = 'ABAP' )
).
TYPES : BEGIN OF ty_int1,
name type char10,
value type char20,
END OF ty_int1,
tt_int1 TYPE STANDARD TABLE OF ty_int1 WITH DEFAULT KEY,
BEGIN OF ty_deep,
region type char5,
country type char10,
ls_work_info type tt_int1,
END OF ty_deep,
tt_deep TYPE STANDARD TABLE OF ty_deep WITH DEFAULT KEY.
DATA(lt_deep) = VALUE tt_deep(
(
region = 'APJ' country = 'INDIA'
ls_work_info = VALUE #(
( name = 'TECH' value = 'SAP' )
( name = 'DOMAIN' value = 'ABAP' )
)
) " first row
(
region = 'EMEA' country = 'UK'
ls_work_info = VALUE #(
( name = 'TECH' value = 'ML' )
( name = 'DOMAIN' value = 'PYTHON' )
)
) " second row
).
Now as our internal table is ready , lets do some manipulations based on above created internal table :
*OLD
DATA : ls_int1 TYPE ty_int1.
READ TABLE lt_int1 INTO ls_int1 INDEX 2.
3.1.2. Read table with keys
*OLD
READ TABLE lt_int1 INTO ls_int1 WITH KEY name = 'TECH'
value = 'SAP'.
*NEW
DATA(ls_int3) = lt_int1[ name = 'TECH' value = 'SAP' ].
*OLD
READ TABLE lt_int1 WITH KEY name = 'TECH'
value = 'SAP'
TRANSPORTING NO FIELDS.
*NEW
IF line_exists( lt_int1[ name = 'TECH' value = 'SAP' ] ).
WRITE : 'Line exists'.
ENDIF.
*If line entry exists ( which is there in our case ) , control comes inside IF
3.1.4. Reading and passing value to a variable
* OLD
DATA lv_1 TYPE char10.
READ TABLE lt_int1 INTO ls_int1 WITH KEY value = 'SAP'.
IF sy-subrc IS INITIAL.
lv_1 = ls_int1-name.
ENDIF.
* NEW
DATA(lv_2) = lt_int1[ value = 'SAP' ]-name.
3.1.5. Changing data in the table
*OLD
READ TABLE lt_int1 INTO ls_int1 WITH KEY name = 'TECH'
value = 'SAP'.
* OLD
* Changing/Accessing value in internal table with deep structure
* Lets take example of the previously created lt_deep table
to fetch data from value column having region as ‘APJ’ and name as ‘TECH’
DATA : ls_deep LIKE LINE OF lt_deep,
ls_work LIKE LINE OF ls_deep-ls_work_info.
READ TABLE lt_deep INTO ls_deep WITH KEY region = 'APJ'.
IF sy-subrc IS INITIAL.
READ TABLE ls_deep-ls_work_info INTO ls_work WITH KEY name = 'TECH'.
lv_1 = ls_work-value.
ENDIF.
*NEW
TRY.
DATA(lv_3) = lt_deep[ region = 'APJ' ]-ls_work_info[ name = 'TECH' ]-value.
CATCH cx_sy_itab_line_not_found.
ENDTRY.
Hence always put the piece of code inside TRYCATCH and catch the exception 'cx_sy_itab_line_not_found' as shown above OR you can also use
LINE_EXISTS check and then proceed with the code.
No comments:
Post a Comment