检查itab2中是否存在itab1行?
Check whether an itab1 lines exist in itab2?
我有 2 个内部 tables typeв TYPE STANDARD TABLE OF string
第一个table包含数据
| K1 | K2 | K3 |
第二个table包含数据
| K1 | K2 |
在 ABAP 中,检查第二个 table 条目是否完全存在于第一个 table 中的最快方法是什么?也许一些内联函数?
DATA lt_current_values TYPE stringtab.
DATA lt_previous_values TYPE stringtab.
APPEND 'K1' TO lt_previous_values.
APPEND 'K2' TO lt_previous_values.
APPEND 'K3' TO lt_previous_values.
APPEND 'K1' TO lt_current_values.
APPEND 'K2' TO lt_current_values.
DATA lv_count TYPE i.
LOOP AT lt_current_values INTO DATA(ls_cur).
READ TABLE lt_previous_values TRANSPORTING NO FIELDS WITH KEY table_line = ls_cur.
IF sy-subrc = 0.
lv_count += 1.
ENDIF.
ENDLOOP.
IF lv_count = LINES( lt_current_values ).
" present => current_table values are already present in previous_table
ENDIF.
解决方案是临时使用 FILTER
的哈希表。
DATA lt_current_values TYPE stringtab.
FIELD-SYMBOLS <ls_current_values> LIKE LINE OF lt_current_values.
DATA lt_previous_values TYPE stringtab.
FIELD-SYMBOLS <ls_previous_values> LIKE LINE OF lt_previous_values.
DATA lth_current_values LIKE HASHED TABLE OF <ls_current_values> WITH UNIQUE KEY table_line.
DATA lth_previous_values LIKE HASHED TABLE OF <ls_previous_values> WITH UNIQUE KEY table_line.
lt_previous_values = VALUE #( ( `K1` ) ( `K2` ) ( `K3` ) ).
lt_current_values = VALUE #( ( `K1` ) ( `K2` ) ).
lth_current_values = lt_current_values.
lth_previous_values = lt_previous_values.
DATA(lt_difference) = FILTER #( lth_current_values EXCEPT IN lth_previous_values WHERE table_line = table_line ).
IF lines( lt_difference ) = 0.
BREAK-POINT.
" present => current_table values are already present in previous_table
ENDIF.
我建议对您的代码进行 2 处更改:
DATA lt_current_values TYPE stringtab.
DATA lt_previous_values TYPE stringtab.
APPEND 'K1' TO lt_previous_values.
APPEND 'K2' TO lt_previous_values.
APPEND 'K3' TO lt_previous_values.
APPEND 'K1' TO lt_current_values.
APPEND 'K2' TO lt_current_values.
SORT lt_previous_values. " Sort the table to use binary search
DATA lv_count TYPE i.
LOOP AT lt_current_values INTO DATA(ls_cur).
READ TABLE lt_previous_values TRANSPORTING NO FIELDS WITH KEY table_line = ls_cur BINARY SEARCH. " use Binary Search
IF sy-subrc NE 0. " Check if it DOESN'T exist
DATA(lv_not_contains_all) = abap_true. " Use a flag to check later
EXIT. " Don't need to continue checking, this can save a lot of time
ENDIF.
ENDLOOP.
IF lv_not_contains_all IS INITIAL.
" present => current_table values are already present in previous_table
ENDIF.
我有 2 个内部 tables typeв TYPE STANDARD TABLE OF string
第一个table包含数据
| K1 | K2 | K3 |
第二个table包含数据
| K1 | K2 |
在 ABAP 中,检查第二个 table 条目是否完全存在于第一个 table 中的最快方法是什么?也许一些内联函数?
DATA lt_current_values TYPE stringtab.
DATA lt_previous_values TYPE stringtab.
APPEND 'K1' TO lt_previous_values.
APPEND 'K2' TO lt_previous_values.
APPEND 'K3' TO lt_previous_values.
APPEND 'K1' TO lt_current_values.
APPEND 'K2' TO lt_current_values.
DATA lv_count TYPE i.
LOOP AT lt_current_values INTO DATA(ls_cur).
READ TABLE lt_previous_values TRANSPORTING NO FIELDS WITH KEY table_line = ls_cur.
IF sy-subrc = 0.
lv_count += 1.
ENDIF.
ENDLOOP.
IF lv_count = LINES( lt_current_values ).
" present => current_table values are already present in previous_table
ENDIF.
解决方案是临时使用 FILTER
的哈希表。
DATA lt_current_values TYPE stringtab.
FIELD-SYMBOLS <ls_current_values> LIKE LINE OF lt_current_values.
DATA lt_previous_values TYPE stringtab.
FIELD-SYMBOLS <ls_previous_values> LIKE LINE OF lt_previous_values.
DATA lth_current_values LIKE HASHED TABLE OF <ls_current_values> WITH UNIQUE KEY table_line.
DATA lth_previous_values LIKE HASHED TABLE OF <ls_previous_values> WITH UNIQUE KEY table_line.
lt_previous_values = VALUE #( ( `K1` ) ( `K2` ) ( `K3` ) ).
lt_current_values = VALUE #( ( `K1` ) ( `K2` ) ).
lth_current_values = lt_current_values.
lth_previous_values = lt_previous_values.
DATA(lt_difference) = FILTER #( lth_current_values EXCEPT IN lth_previous_values WHERE table_line = table_line ).
IF lines( lt_difference ) = 0.
BREAK-POINT.
" present => current_table values are already present in previous_table
ENDIF.
我建议对您的代码进行 2 处更改:
DATA lt_current_values TYPE stringtab.
DATA lt_previous_values TYPE stringtab.
APPEND 'K1' TO lt_previous_values.
APPEND 'K2' TO lt_previous_values.
APPEND 'K3' TO lt_previous_values.
APPEND 'K1' TO lt_current_values.
APPEND 'K2' TO lt_current_values.
SORT lt_previous_values. " Sort the table to use binary search
DATA lv_count TYPE i.
LOOP AT lt_current_values INTO DATA(ls_cur).
READ TABLE lt_previous_values TRANSPORTING NO FIELDS WITH KEY table_line = ls_cur BINARY SEARCH. " use Binary Search
IF sy-subrc NE 0. " Check if it DOESN'T exist
DATA(lv_not_contains_all) = abap_true. " Use a flag to check later
EXIT. " Don't need to continue checking, this can save a lot of time
ENDIF.
ENDLOOP.
IF lv_not_contains_all IS INITIAL.
" present => current_table values are already present in previous_table
ENDIF.