3.1. Manipulations - Internal Table

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.

 
Internal Table 
 
To understand various operations on internal table lets start with creating and filling data in internal table with simple approach.
 
Old approach  : 
 TYPES : BEGIN OF ty_int1                   
               name type char10, 
               value type char20, 
               END OF ty_int1.
 
  DATA : lt_int1 type STANDARD TABLE OF ty_int1,
                ls_int1 TYPE ty_int1   .  
           
  ls_int1-name = 'TECH'.
  ls_int1-value = 'SAP'.
  APPEND ls_int1 TO lt_int1.
 
  ls_int1-name = 'DOMAIN'.
  ls_int1-value = 'ABAP'.
  APPEND ls_int1 TO lt_int1.


 New approach :
  TYPES : BEGIN OF ty_int1,
          name type char10,
          value type char20,
          END OF ty_int1.

  TYPES : tt_int1 type STANDARD TABLE OF ty_int1 with DEFAULT KEY.   
                 " table type of structure 
 
 
 " Need to specify the type as tt_int1 after VALUE keyword because     internal table structure is not defined
  DATA(lt_int1) = VALUE tt_int1(                                  
                                 ( name = 'TECH' value = 'SAP' )
                                 ( name = 'DOMAIN' value = 'ABAP' )
                                ).
                                  OR
  TYPES : BEGIN OF ty_int1,
          name type char10,
          value type char20,
          END OF ty_int1.
  DATA : lt_int1 type STANDARD TABLE OF ty_int1 with DEFAULT KEY.   
               " internal table

" Use '#' as internal table structure is defined 
  lt_int1 = VALUE #(                                         
                    ( name = 'TECH' value = 'SAP' )
                    ( name = 'DOMAIN' value = 'ABAP' )
                    ).
Created Table :

 
Creating internal table with deep structure
 

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 :

3.1.1. Read table based on index 

*OLD
DATA : ls_int1 TYPE ty_int1.
READ TABLE lt_int1 INTO ls_int1 INDEX 2.

*NEW  
 DATA(ls_int12) = lt_int1[ 2 ].  
               Read index 2 value of lt_int1 table which we created earlier.

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' ].

 
 
3.1.3. Check if value is present in table
 

*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'.
 

  IF sy-subrc IS INITIAL.
                       ls_int1-name = 'TECHNOLOGY'.
                       “ Modify internal table from workarea 
                ENDIF.
 
            * NEW  
                 lt_int1[ name = 'TECH' ]-name = 'TECHNOLOGY'.
 
 
 
3.1.6. Reading table with deep structure

* 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.

 

 
 
 
 
NOTE:
With the new syntax if the table entry is not found with the key values 
then it will throw an exception.
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