问题
I have a set of values: "foo", "bar", "blue".
I have a table which looks like this:
ID | my_col
-----------
1 | foo
2 | bar
I want the set values minus all available my_col values.
[foo, bar, blue] minus [foo, bar]
The result should be "blue".
How to do this in ABAP?
回答1:
Here you are...
REPORT YYY.
TYPES string_table TYPE HASHED TABLE OF string WITH UNIQUE KEY TABLE_LINE.
DATA(gt_set1) = VALUE string_table( ( `foo` ) ( `bar` ) ( `blue` ) ).
DATA(gt_set2) = VALUE string_table( ( `foo` ) ( `bar` ) ).
DATA(gt_set1_except_set2) = FILTER string_table( gt_set1 EXCEPT IN gt_set2 WHERE table_line = table_line ).
Works however only with HASHED
and SORTED
tables.
回答2:
a couple of additional examples with standard tables:
data: set type table of string, " initial set
tab type table of string, " you table
res type table of string. " the result
set = value #( ( `foo` ) ( `bar` ) ( `blue` ) ).
tab = value #( ( `foo` ) ( `bar` ) ).
Option 1: assuming initial set and tab are standard table you can simply loop for the initial set, and then look into your table values
In this case a full table search is done in tab table -> O(n) for tab search
LOOP AT set into data(lv_set).
read table tab from lv_set transporting no fields.
check sy-subrc > 0.
append lv_set to res.
ENDLOOP.
Option 2: you can use a temporary hashed table as described in
SE38 -> Environment -> Performace examples (Intersection of internal tables)
data: htab type hashed table of string with unique key table_line.
htab = tab. " use Hashed table as temporary working table
loop at set into lv_set.
" fast table lookup accessing with unique key O(1)
read table htab from lv_set transporting no fields.
check sy-subrc > 0.
append lv_set to res.
endloop.
free htab.
Best regards !
来源:https://stackoverflow.com/questions/54907235/difference-of-two-sets-of-values