2011 Jul 28 9:04 AM
Hi Experts,
I have the following problem now:
I have 2 internal tables : Tab A and Tab B , both of the same type.
Tab A has say 10 entries and Tab B say 8. Now i want to put the 10 -8 = 2 records in Tab C. And Tab c is also of the same type as Tab A and B.
I know it can be achieved by loops but i want to avoid usage of loops and read. Is there a way.
Thanks!
Gayathri
Hi Experts,
I have the following problem now:
I have 2 internal tables : Tab A and Tab B , both of the same type.
Tab A has say 10 entries and Tab B say 8. Now i want to put the 10 -8 = 2 records in Tab C. And Tab c is also of the same type as Tab A and B.
I know it can be achieved by loops but i want to avoid usage of loops and read. Is there a way.
Thanks!
Gayathri
2011 Jul 28 10:17 AM
Hi,
This is highly possible. Use function module BKK_COMPARE_TABLES. Please check the following code:
DATA: itab1 TYPE STANDARD TABLE OF mara,
itab2 TYPE STANDARD TABLE OF mara,
itab3 TYPE STANDARD TABLE OF mara,
wa TYPE mara.
*Add 1 entry to itab1
wa-matnr = 38.
APPEND wa TO itab1.
*Get the difference of itab1 & itab2 into itab3
CALL FUNCTION 'BKK_COMPARE_TABLES'
EXPORTING
i_tab = itab1
i_tab_sub = itab2
IMPORTING
e_tab_difference = itab3.
LOOP AT itab3 INTO wa.
WRITE: wa-matnr.
ENDLOOP.
Hope this would help.
BR,
Atanu
2011 Jul 28 11:46 AM
Hi Gayathri,
Are you sure that your TAB B will contain same entries like in TAB A but only difference is the entries in TAB B will be less than TAB A ?
Regards
HM
2011 Jul 28 1:29 PM
Hi Gayathri,
it depends on what you really want:
If you just want the lines that tab B has more than tab A, then
data:
tab_c like tab_a[],
lines_more type sy-tabix.
lines_more = lines( tab_a ) + 1.
APPEND LINES OF tab_a to tab_c from lines_more.If you want to know the difference betweeen the tables, that is the lines that the tables do not have in common, then the approach of Atanu Mukherjee is not enough. Not only the function BKK_COMPARE_TABLES uses LOOP, SORT and read table. Also it will give you just the lines of tab a that are not in tab b but not those lines of tab b that are not included in tab a.
If you tell us, why you need it and what is the reason to avoid loop, probably we have an idea.
Regards
Clemens
2011 Jul 28 2:38 PM
You should consider the complexity of algorithm not worry about using the loops.
In general it depends on the types of the tables.
Anyway you have to iterate over first array which is O(m).
Depending on the type of second table you can do the look up in:
O(1) for hashed table
O(logN) for sorted table
O(N) for standard table.
So your solution can be O(n*m) or O(n) depending on table types.
Tomek