Use VALUE and FOR to Transfer Internal Table Entries

Use VALUE and FOR to Transfer Internal Table Entries

Use VALUE AND FOR to Transfer Internal Table Entries.

For example, we have an internal table with 2 columns, employee name and employee role. And I want to transfer only employee names from the internal table to another internal table. To Transfer any column entries from one internal table to another internal table we generally use LOOP statements and then append the required field to new internal table.

With ABAP 7.4, we can shorten the code using VALUE and FOR. Check the below example.

Code:

REPORT ztest_program.

*--Structure
TYPES: BEGIN OF ty_tab,
         empname TYPE string,
         role    TYPE string,
       END OF ty_tab.

*--Table Type
TYPES: tt_emp TYPE STANDARD TABLE OF string WITH DEFAULT KEY.
DATA: gt_tab TYPE TABLE OF ty_tab.
*DATA: it_emp TYPE TABLE OF string.

*--Populate Internal Table
gt_tab = VALUE #( ( empname = 'rohit'  role = 'java' )
                  ( empname = 'dhawan' role = 'abap' )
                  ( empname = 'dinesh' role = 'basis')
                  ( empname = 'bhuvi'  role = 'abap' )
                ).

*--Extract EMPNAME from GT_TAB Internal Table
*-- BEFORE ABAP 7.4
*LOOP AT gt_tab INTO DATA(gs_tab).
*  DATA(gv_empname) = gs_tab-empname.
*  APPEND gv_empname TO it_emp.
*ENDLOOP.

*-- WITH ABAP 7.4
DATA(it_emp) = VALUE tt_emp( FOR ls_tab IN gt_tab ( ls_tab-empname ) ).

*-- Using Where Condition
*DATA(it_emp) = VALUE tt_emp( FOR ls_tab IN gt_tab WHERE ( role = 'abap' ) ( 
ls_tab-empname ) ).

cl_demo_output=>display( it_emp ).
Output:
IT_EMP
rohit
dhawan
dinesh
bhuvi

Post a Comment

Previous Post Next Post