on 02-22-2010 2:01 AM
ABAP提取两个内表交集
我有注意到,SAP自带的函数:
CTVB_COMPARE_TABLES和BKK_COMPARE_TABLES;
似乎可以比较两个内表,得出第二个内表不同于第一个内表的部分(新增/删除了那些部分)
但是,具体的使用,还请有经验的朋友不吝赐教啊!
因为,我在测试数据时,发现这两个函数的效果不那么简单。
如果上述函数确实可以,提取两个内表不同部分,则我可以据此做两次比较,得到两个内表的交集。
所以,我先用另外一种方式解决了-自己写了一个提取两个内表交集的函数,供大家检阅:
*" IMPORTING
*" VALUE(ITAB1) TYPE INDEX TABLE
*" VALUE(ITAB2) TYPE INDEX TABLE
*" EXPORTING
*" VALUE(ITABSAME) TYPE INDEX TABLE
*"----
field-symbols:
<S1>,
<S2>.
data:
L1 type i,
L2 type i.
assign local copy of initial line of:
ITAB1 to <S1>,
ITAB2 to <S2>.
describe: table ITAB1 lines L1,
table ITAB2 lines L2.
"对记录行数少的内表,执行第一层循环;
"在第二层循环中,找到对应记录,即可追加到结果内表;
"同时退出第二层循环,继续执行第一层循环的下一行
IF L1 <= L2.
LOOP AT ITAB1 INTO <S1>.
LOOP AT ITAB2 INTO <S2>.
IF <S1> EQ <S2>.
APPEND <S1> TO ITABSAME.
EXIT.
ENDIF.
ENDLOOP.
ENDLOOP.
ELSE.
LOOP AT ITAB2 INTO <S2>.
LOOP AT ITAB1 INTO <S1>.
IF <S1> EQ <S2>.
APPEND <S2> TO ITABSAME.
EXIT.
ENDIF.
ENDLOOP.
ENDLOOP.
ENDIF.
ENDFUNCTION.
另一个问题,想请教大家,在上面代码里,第二层循环是为了找出,第一层循环的当前记录,在第二个内表里是否存在;
所以,如果ABAP自带了,判断一个内表中,是否存在某个记录的函数的话,那第二层循环就可以省去,则上述函数执行效率也会成倍提升了。
Your algorithm seems to have time complexity of O(n^2).
Qiang Sheng's can reach O(nlog(n)).
Besides, if you use SORTED TABLE, things may be even easier.
Am I right? ^^
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
嗯,最好先排序,然后用read table binary search,效率最快
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
算法其实有很多,比如还可以先排序,再进行单次遍历比较即可。
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
98 | |
11 | |
11 | |
10 | |
10 | |
8 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.