‎2008 Feb 18 9:27 AM
hi can any one tell me how to compare two internal tables
i have itab with 1,2,3,4,5 and
in itab1 i have 2,4,6,8,9
i compare both
and i want 2 get the output like 1,3,5,6,7,8,9
i.e which r same in itab and itab1 were do not dispaly
give me logic
Edited by: Alvaro Tejada Galindo on Feb 18, 2008 2:27 PM
‎2008 Feb 18 9:32 AM
Hello,
Do like this.
loop at itab.
read table itab1 with key field1 = itab-field1.
if sy-subrc ne 0.
write:\ itab-field1.
endif.
endloop.
Cheers,
Vasanth
‎2008 Feb 18 9:32 AM
Hello,
Do like this.
loop at itab.
read table itab1 with key field1 = itab-field1.
if sy-subrc ne 0.
write:\ itab-field1.
endif.
endloop.
Cheers,
Vasanth
‎2008 Feb 18 9:33 AM
hi,
there are so many ways it might be easy logic..
loop at itab1.
append itab1 to itab2.
endllop.
so u got all the records into one table..
then wht u do is sort that table with key as the field that conatines the number..
then finalyy eliminate all the duplicates form the second internal table and u will get wht the records u want i hope i am clear to u.
ex...
‎2008 Feb 18 9:34 AM
hi
i am supposing that 1,2,3, 4,5 etc are values in a column of table.
not you can do it like this.........
loop at itab into wa_itab.
read table itab1 into wa_itab1 with key f1 = wa_itab-f1.
if sy-subrc ne 0.
append wa_itab1 to itab2.
endif.
endloop.
regards
vijay
<REMOVED BY MODERATOR>
Edited by: Alvaro Tejada Galindo on Feb 18, 2008 2:28 PM
‎2008 Feb 18 9:35 AM
Hi,
Take one more temparory internal table and append these 2 internal tables data to that. Then sort the temp table with key and do delete adjacent duplicates.
APPEND LINES ITAB1[] to ITAB_TEMP[].
APPEND LINES ITAB2[] TO ITAB_TEMP[].
SORT ITAB_TEMP[] BY field1....<Key>.
DELETE ADJACENT DUPLICATES FROM ITAB_TEMP COMPARING field1.....<Key>
Rgds,
Bujji
‎2008 Feb 18 9:37 AM
for example if
first table has records like 1, 2, 3
second table has records like 2,4,5.
then combine those as
1,2,3,2,4,5.
now by sort do like this..
1, 2, 2, 3, 4, 5.
now eliminate adjacent duplicates like this..
1, 2, 3, 4, 5.
u r problem solved
<REMOVED BY MODERATOR>
Edited by: Alvaro Tejada Galindo on Feb 18, 2008 2:28 PM
‎2008 Feb 18 9:40 AM
Sort table itab2 by k1 k2 k3..
loop at itab1.
read table itab2 with key k1 = itab1-k1
k2 = itab1-k2 binary search.
if sy-subrc = 0.
delete itab2 index sy-index.
endif.
endloop.
finally itab2 will have that sort of entries which are not in Itab1.
<REMOVED BY MODERATOR>
Edited by: Alvaro Tejada Galindo on Feb 18, 2008 2:29 PM
‎2008 Feb 18 10:13 AM
data:begin of itab1 occurs 0,
val type i,
flg type c,
end of itab1,
itab2 like table of itab1 with header line,
itab3 like table of itab1 with header line.
itab1-val = 1.
append itab1.
itab1-val = 2.
append itab1.
itab1-val = 3.
append itab1.
itab1-val = 4.
append itab1.
itab1-val = 5.
append itab1.
itab2-val = 5.
append itab2.
itab2-val = 7.
append itab2.
itab2-val = 8.
append itab2.
itab2-val = 2.
append itab2.
itab2-val = 1.
append itab2.
Loop at itab1.
read table itab2 with key val = itab1-val.
if sy-subrc = 0.
itab2-flg = 'X'.
modify itab2 index sy-tabix transporting flg.
else.
itab3-val = itab1-val.
append itab3.
endif.
at last.
loop at itab2 where flg <> 'X'.
itab3-val = itab2-val.
append itab3.
endloop.
endat.
endloop.
sort itab3.
Now itab3 has ur values
‎2008 Feb 18 10:15 AM
Sunil Kumar idea is ver ggod..try that.
append lines of itab1 to itab3.
append lines of itab2 to itab3.
sort itab3.
delete adjacent duplicates from itab3.
‎2008 Feb 18 10:59 AM
combine both the numbers and delete the adjacent duplicates.
next read the no from 1st table and 2 nd table if it exits in both the table then delete or else display
‎2008 Feb 18 11:04 AM
Hi,
Try the FM
CATSXT_COMPARE_STRUCTURES
CTVB_COMPARE_TABLES
However I think you might code it faster with a line-by-line comparison, within a LOOP.
Regards,
Chandru
‎2008 Feb 18 11:38 AM
hi friend,
u r not mention the input 7 in any one of the table. no probs.
here it my logic given below.
REPORT YTEST.
data : begin of it1 occurs 10,
n1 type i,
end of it1,
begin of it2 occurs 10,
n2 type i,
end of it2,
begin of it3 occurs 10,
n3 type i,
end of it3.
data: flag type i.
it1-n1 = 1.
append it1.
it1-n1 = 2.
append it1.
it1-n1 = 3.
append it1.
it1-n1 = 4.
append it1.
it1-n1 = 5.
append it1.
it2-n2 = 2.
append it2.
it2-n2 = 4.
append it2.
it2-n2 = 6.
append it2.
it2-n2 = 8.
append it2.
it2-n2 = 9.
append it2.
loop at it1 into it1.
loop at it2 into it2.
if it1-n1 = it2-n2.
flag = 0.
exit.
else .
flag = 1.
endif.
endloop.
if flag = 1.
append it1 to it3.
endif.
endloop.
loop at it2 into it2.
loop at it1 into it1.
if it2-n2 = it1-n1.
flag = 0.
exit.
else .
flag = 1.
endif.
endloop.
if flag = 1.
append it2 to it3.
endif.
endloop.
loop at it3 into it3.
write:/ it3-n3.
note : if the resultant internal table is not sorted form. sort it and then view the answer.
regards,
surender
‎2008 Feb 18 7:37 PM
Hi Upender,
try it this way.
data: v_count type i,
v_tabix type sy-tabix.
itab2[] = itab[].
append lines of itab1 to itab2.
sort itab2 by value.
loop at itab2.
v_tabix = sy-tabix.
v_count = v_count + 1.
at end of value.
clear : v_count.
endat.
if v_count > 2.
delete itab2 where value = itab2-value.
endif.
endloop.
now itab2 will hold unduplicated values.
Revert back for more help.
Regards,
Naresh