10-31-2007 9:43 PM
Hi All,
My problem is I have two files with same structure, but may be having different data. I want to compare those files. I can read those files into two internal tables.
What is the fastest way to compare and determine the differences between the internal tables?
Thanks,
Aravind
11-02-2007 3:06 PM
The following coding should be a guidline for an efficient compare of two internal table, using a hashed table for one of the two tables.
It assumes that there new lines, disappeared and chnages lines.
The three table ins_tab, del_tab, upd_tad collect the differences, they must be applied to itab1 to change it into itab2.
Siegfried
data:
tab1 type tab,
tab2 type hash.
refresh ins_tab.
refresh del_tab.
refresh upd_tab.
tab1[] = itab1[].
tab2[] = itab2[].
clear wa1.
clear wa2.
loop at tab1 into wa1.
read table tab2 into wa2
with table key keyfield = wa1-keyfield.
index1 = sy-tabix.
* entry not in itab2
if ( sy-subrc ne 0 ).
append wa1 to del_tab.
* entry in itab2, update if different
else.
delete table tab2
with table key keyfield = wa1-keyfield.
if ( wa1-ctext ne wa2-ctext ).
append wa2 to upd_tab.
endif.
endif.
endloop.
* in itab2 but not in itab1
ins_tab[] = tab2[].
10-31-2007 9:46 PM
I guess you could read them into the two internal tables and sort them. Then loop through one table and read the corresponding record of the second table. Something like:
sort: itab1, itab2.
loop at itab.
read itab2 index sy-tabix.
if itab1 <> itab2.
Do something.
endif.
endloop.
Rob
10-31-2007 10:32 PM
first sort both the internal tables using some sort key.
then check...
if itab1[] = itab2[].
.....
endif.
Reward if it is useful.
Thanks,
Srinivas
11-02-2007 3:06 PM
The following coding should be a guidline for an efficient compare of two internal table, using a hashed table for one of the two tables.
It assumes that there new lines, disappeared and chnages lines.
The three table ins_tab, del_tab, upd_tad collect the differences, they must be applied to itab1 to change it into itab2.
Siegfried
data:
tab1 type tab,
tab2 type hash.
refresh ins_tab.
refresh del_tab.
refresh upd_tab.
tab1[] = itab1[].
tab2[] = itab2[].
clear wa1.
clear wa2.
loop at tab1 into wa1.
read table tab2 into wa2
with table key keyfield = wa1-keyfield.
index1 = sy-tabix.
* entry not in itab2
if ( sy-subrc ne 0 ).
append wa1 to del_tab.
* entry in itab2, update if different
else.
delete table tab2
with table key keyfield = wa1-keyfield.
if ( wa1-ctext ne wa2-ctext ).
append wa2 to upd_tab.
endif.
endif.
endloop.
* in itab2 but not in itab1
ins_tab[] = tab2[].