Intersection of internal tables

来源:互联网 发布:广东网络作协 编辑:程序博客网 时间:2024/06/05 08:16

Using a sorted table temporarily
Runtime: 101 microseconds


* Entries: 200 (ITAB1), 100 (ITAB2)
* Intersection: 50 (ITAB3)
* Line width: 100, Key width: 20

STAB1 = ITAB1.
REFRESH ITAB3.
LOOP AT ITAB2 ASSIGNING <WA>.
  READ TABLE STAB1 FROM <WA>
                   TRANSPORTING NO FIELDS.
  IF SY-SUBRC = 0.
    APPEND <WA> TO ITAB3.
  ENDIF.
ENDLOOP.
FREE STAB1.


Using a hashed table temporarily
Runtime: 92 microseconds
* Entries: 200 (ITAB1), 100 (ITAB2)
* Intersection: 50 (ITAB3)
* Line width: 100, Key width: 20

HTAB1 = ITAB1.
REFRESH ITAB3.
LOOP AT ITAB2 ASSIGNING <WA>.
  READ TABLE HTAB1 FROM <WA>
                   TRANSPORTING NO FIELDS.
  IF SY-SUBRC = 0.
    APPEND <WA> TO ITAB3.
  ENDIF.
ENDLOOP.
FREE HTAB1.

 


Documentation
The source tables ITAB1 and ITAB2 are standard tables. It is assumed
that ITAB1 takes more entries than ITAB2. Otherwise, the table with
more entries must be computed with "DESCRIBE TABLE ... LINES ...".
Since both tables shall represent sets, it is assumed that their
entries are unique with respect to component K.

The algorithm works with a temporary table with unique key K. The
table is a copy of ITAB1 and is used to locate the entries beeing
also contained in ITAB2. The matching entries are copied to ITAB3.
The left-hand and right-hand side differ only by the kind of the
temporary table being used. For a hashed table, the READ statement
in the LOOP is faster than for the sorted table.

原创粉丝点击