FILTER
As name suggests , it can be used to filter out rows containing columns with particular values. It can be used as replacement of passing few records from one table to another inside loop statements.
So again we are going to use here the structures created previously but with few changes
* Filter can only be used on tables containing either sorted or hashed keys
TYPES : BEGIN OF ty_source,
name_source TYPE char10,
value_source TYPE char20,
END OF ty_source,
tt_source TYPE SORTED TABLE OF ty_source WITH UNIQUE KEY name_source,
BEGIN OF ty_target,
name TYPE char10,
value TYPE char20,
exp TYPE i,
END OF ty_target,
tt_target TYPE SORTED TABLE OF ty_target WITH non-UNIQUE KEY name .
DATA(lt_source) = VALUE tt_source(
( name_source = 'TECH' value_source = 'SAP' )
( name_source = 'DOMAIN' value_source = 'ABAP' )
).
DATA(lt_target) = VALUE tt_target(
( name = 'TECH2' value = 'SAP2' exp = '5' )
( name = 'DOMAIN2' value = 'ABAP2' exp = '10')
( name = 'TECH' value = 'SAP' exp = '20')
( name = 'TECH3' value = 'TECH' exp = '15' )
( name = 'DOMAIN3' value = 'ABAP3' exp = '10')
).
Created tables :
3.3.1. Filter on single table
It will give only the row having matching column.
DATA : lv_name TYPE char10 VALUE 'TECH'.
lt_target = FILTER #( lt_target WHERE name = lv_name ).
3.3.2. FILTER with EXCEPT
It will give all other rows except the row containing matching column
lt_target = FILTER #( lt_target EXCEPT WHERE name = lv_name ).
3.3.3. Filter based on two tables
DATA(lt_filter) = FILTER tt_target( lt_target in lt_source
WHERE name = name_source ).
* Using Except
lt_filter = FILTER #( lt_target EXCEPT in lt_source
WHERE name = name_source ).
No comments:
Post a Comment