Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to get difference between 2 nternal tables

Former Member
0 Likes
717

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

4 REPLIES 4
Read only

atanu_mukherjee
Explorer
0 Likes
651

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

Read only

Former Member
0 Likes
651

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

Read only

Clemenss
Active Contributor
0 Likes
651

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

Read only

former_member182670
Contributor
0 Likes
651

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