‎2005 Jul 01 11:58 AM
Hi,
I have 2 internal tables, say, A & B. The records in B is subset of records in A. Now I want to find out the set of records which are in A, but not in B. I'm planning to do like below(pseudocode). Any other optimized way possible?
LOOP at A.
READ table B (using binary search).
IF sy-subrc = 0.
...
ELSE.
...
ENDIF.
ENDLOOP.
Thanks & Regards,
Sree
‎2005 Jul 01 12:34 PM
Hi,
If i assume that ur internal tables have a key field to identify the unique record for example material number.
then...
Loop at itab2.
populate the key field value into a range table r_matnr
with other fields like opt = 'EQ' and sign = 'I'.
endloop.
Now u can use.
delete itab1 where not matnr in r_matnr.
this is advisable if the entries are very high,
Or ur code can be optimised as...
Since ur saying that B is subset of A. (assuming that number of entries in B comparatively less than in A)
u can do..
Loop at B.
Read table A (using binary search).
if sy-subrc eq 0.
delte table A index sy-tabix.
endif.
endloop.
‎2005 Jul 01 12:34 PM
Hi,
If i assume that ur internal tables have a key field to identify the unique record for example material number.
then...
Loop at itab2.
populate the key field value into a range table r_matnr
with other fields like opt = 'EQ' and sign = 'I'.
endloop.
Now u can use.
delete itab1 where not matnr in r_matnr.
this is advisable if the entries are very high,
Or ur code can be optimised as...
Since ur saying that B is subset of A. (assuming that number of entries in B comparatively less than in A)
u can do..
Loop at B.
Read table A (using binary search).
if sy-subrc eq 0.
delte table A index sy-tabix.
endif.
endloop.
‎2005 Jul 01 1:08 PM
Hi Sharath,
Using a ranges table is an impressive solution. And I think it is the best approach, primarily due to the fact that there's only one LOOP (to create a range).
Sree: This is a good solution. You must try implementing it in case your internal table has a key comrpising a single field.
Regards,
Anand Mandalika.
‎2005 Jul 01 1:39 PM
Thanks you all! very interesting solutions I got which I was looking for !
In my case, I have to go with Patrik's suggestions as my table has 2 non-unique keys.
I will keep the use of Ranges in my mind for some other place !
Thanks & Regards,
Sree
‎2005 Jul 01 1:47 PM
On doing little more analysis, I found that there will be only one key for the Internal table and hence usign Sharath's solution!
Thanks all again!
Regards,
Sree
‎2005 Jul 01 12:43 PM
Hi Sree,
looks like , that there's nothing to optimize.
regards Andreas
‎2005 Jul 01 12:50 PM
Sree,
I stumbled around in the ABAP documentation trying to find a suitable command for a 'difference set' operation - because that is essentially what you are trying to achieve - but I did not succeed.
Nevertheless when analyzing the computational complexity of your program one can say that it is very efficient.
Binary search is really fast (log(n) precisely) but can only be applied to comparable, ordered and properly sorted data. Assuming this is the case with B then your algorithm has a total complexity of n*log(n) which is good.
There are nevertheless two optimizations I would like to present to you:
1. Do the binary search in the larger of the two tables and consequentially loop over the smaller one.
2. Concerning the 'target' of your operation: The entries that you search for (i.e. the ones that do not exist in both tables) will probably be written into a table C. It makes sense to create this table with an explicit initial size which is max(size(A),size(B))-min(size(A),size(B)) (pseudocoded, size(A) and size(B) of course in local variables ) to prevent continuous memory allocation which is very time consuming in comparison to the search algorithm
Regards,
Patrick