Application Development 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: 

Comparing two files

Former Member
0 Kudos

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

1 ACCEPTED SOLUTION

former_member194613
Active Contributor
0 Kudos

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[].

3 REPLIES 3

Former Member
0 Kudos

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

0 Kudos

first sort both the internal tables using some sort key.

then check...

if itab1[] = itab2[].

.....

endif.

Reward if it is useful.

Thanks,

Srinivas

former_member194613
Active Contributor
0 Kudos

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[].