FILTER Operator in ABAP Internal Tables

FILTER Operator in ABAP Internal Tables

FILTER operator is used to filter entries based on any field in internal table and pass it to another internal table. To use filter on any internal table the internal table should be of Sorted or Hashed type with key. Prior to ABAP 7.4 to filter internal table entries, we used to LOOP internal table with WHERE condition and then APPEND the filtered entry to new internal table. With new syntax we don’t need to use work area to append the filtered entry to new internal table. We can directly pass the filtered entries to new internal table using FILTER operator.

Check the below example.

CODE

REPORT ztestppk. TYPES: BEGIN OF ty_players,          id        TYPE char4,          name      TYPE char20,          country   TYPE char20,          cntrycode TYPE i,        END OF ty_players. DATA: it_players TYPE SORTED TABLE OF ty_players WITH NON-UNIQUE KEY cntrycode. DATA: it_filter TYPE SORTED TABLE OF i WITH UNIQUE KEY table_line. it_players = VALUE #(                       ( id = '45' name = 'ROHIT'   country = 'INDIA'     cntrycode = 100 )                       ( id = '2'  name = 'MENDIS'  country = 'SRILANKA'  cntrycode = 101 )                       ( id = '31' name = 'WARNER'  country = 'AUSTRALIA' cntrycode = 102 )                       ( id = '54' name = 'BHANUKA' country = 'SRILANKA'  cntrycode = 101 )                       ( id = '18' name = 'KOHLI'   country = 'INDIA'     cntrycode = 100 )                       ( id = '56' name = 'STARC'   country = 'AUSTRALIA' cntrycode = 102 )                     ). cl_demo_output=>write( it_players ). *-----Filter Single Value *-- Filter India Players DATA(it_ind) = FILTER #( it_players WHERE cntrycode = 100 ). cl_demo_output=>write( it_ind ). *-- Filter Srilanka Players DATA(it_sl)  = FILTER #( it_players WHERE cntrycode = 101 ). cl_demo_output=>write( it_sl ). *-- Filter Australia Players DATA(it_aus) = FILTER #( it_players WHERE cntrycode = 102 ). cl_demo_output=>write( it_aus ). *-----Filter Multiple Values *-- Filter India and Australia it_filter = VALUE #( ( 100 ) ( 102 ) ). DATA(it_ind_aus) = FILTER #( it_players IN it_filter WHERE cntrycode = table_line ). cl_demo_output=>display( it_ind_aus ).

Output





Post a Comment

Previous Post Next Post