在 ABAP 中找到两个数据集的差异?

Find a difference of two datasets in ABAP?

我有一组值:"foo"、"bar"、"blue"。

我有一个 table 看起来像这样:

ID | my_col
-----------
 1 | foo
 2 | bar

我想要设置值减去所有可用的 my_col 值。

[foo, bar, blue] 减去 [foo, bar]

结果应该是"blue"。

如何在 ABAP 中执行此操作?

给你...

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

但仅适用于 HASHEDSORTED 表。

几个标准 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` ) ).

选项 1:假设初始集和制表符是标准的 table 您可以简单地循环初始集,然后查看您的 table 值

在这种情况下,完整的 table 搜索是在选项卡中完成的 table -> O(n) 用于选项卡搜索

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.

选项 2:您可以使用临时散列 table,如

中所述

SE38 -> 环境 -> 性能示例(内部 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.

此致!