08-24-2006 4:40 PM
Dear friends,
Could any one of you kindly help me with a code to delete the duplicates in the internal table, but each duplicate should be counted, how many times that appeared and should be displayed again as a report with the messages and no of times that message appeared.
Thank you,
Best Regards,
subramanyeshwer
08-24-2006 4:46 PM
You can try something like this.
report zrich_0001.
data: begin of itab1 occurs 0,
fld1 type c,
fld2 type c,
fld3 type c,
end of itab1.
data: begin of itab2 occurs 0,
fld1 type c,
fld2 type c,
fld3 type c,
end of itab2.
data: counter type i.
itab1 = 'ABC'. append itab1.
itab1 = 'DEF'. append itab1.
itab1 = 'GHI'. append itab1.
itab1 = 'DEF'. append itab1.
itab1 = 'GHI'. append itab1.
itab1 = 'DEF'. append itab1.
itab2[] = itab1[].
sort itab1 ascending.
delete adjacent duplicates from itab1.
loop at itab1.
clear counter.
loop at itab2 where fld1 = itab1-fld1
and fld2 = itab1-fld2
and fld3 = itab1-fld3.
counter = counter + 1.
endloop.
write:/ itab1-fld1, itab1-fld2, itab1-fld3,
'Number of occurances:', counter.
endloop.
Regards,
Rich Heilman
08-24-2006 4:46 PM
You can try something like this.
report zrich_0001.
data: begin of itab1 occurs 0,
fld1 type c,
fld2 type c,
fld3 type c,
end of itab1.
data: begin of itab2 occurs 0,
fld1 type c,
fld2 type c,
fld3 type c,
end of itab2.
data: counter type i.
itab1 = 'ABC'. append itab1.
itab1 = 'DEF'. append itab1.
itab1 = 'GHI'. append itab1.
itab1 = 'DEF'. append itab1.
itab1 = 'GHI'. append itab1.
itab1 = 'DEF'. append itab1.
itab2[] = itab1[].
sort itab1 ascending.
delete adjacent duplicates from itab1.
loop at itab1.
clear counter.
loop at itab2 where fld1 = itab1-fld1
and fld2 = itab1-fld2
and fld3 = itab1-fld3.
counter = counter + 1.
endloop.
write:/ itab1-fld1, itab1-fld2, itab1-fld3,
'Number of occurances:', counter.
endloop.
Regards,
Rich Heilman
08-24-2006 5:41 PM
Hello Rich,
some how its not looping pefectly(inner loop).
I am getting the counter as 1 always.
I could not understand why.
Could you please help.
thank you,
Best Regards,
Subramanyeshwer
08-24-2006 5:42 PM
08-24-2006 6:06 PM
Try this. Catch is that you should not have any number type fields in your internal table.
DATA: BEGIN OF itab1 OCCURS 0,
fld1 TYPE c,
fld2 TYPE c,
fld3 TYPE c.
DATA: END OF itab1.
DATA: BEGIN OF itab2 OCCURS 0,
fld1 TYPE c,
fld2 TYPE c,
fld3 TYPE c,
count TYPE i.
DATA: END OF itab2.
itab1 = 'ABC'. APPEND itab1.
itab1 = 'DEF'. APPEND itab1.
itab1 = 'GHI'. APPEND itab1.
itab1 = 'DEF'. APPEND itab1.
itab1 = 'GHI'. APPEND itab1.
itab1 = 'DEF'. APPEND itab1.
LOOP AT itab1.
MOVE: itab1-fld1 TO itab2-fld1,
itab1-fld2 TO itab2-fld2,
itab1-fld3 TO itab2-fld3,
1 TO itab2-count.
COLLECT itab2.
CLEAR itab2.
ENDLOOP.
LOOP AT itab2.
WRITE:/ itab2-fld1,
itab2-fld2,
itab2-fld3,
itab2-count.
ENDLOOP.
08-25-2006 11:22 AM
Hello Rich,
Perfect.
The problem is solved with the example you provided.
Thanks a lot and I checked the radio button "Problem solved".
Regards,
subramanyeshwer
08-24-2006 4:47 PM
--say your internal table imsg has the field msgkey data.
sort imsg.
loop at imsg.
lcnt = lcnt + 1.
at end of msgkey.
write : / imsg-msgkey, lcnt.
*you can even update it into another internal table and print later.
endat.
endloop.
delete adjacent duplicates from imsg.
*Please check the exact syntax.
--Did you try the above code it should definetly work..
Message was edited by: Anurag Bankley
08-24-2006 4:51 PM
Hi,
sort itab by fieldname.
delete adjacent duplicates from itab comparing fieldname>.
will work fine.
Regards,
Sowjanya.
Message was edited by: sowjanya suggula
08-24-2006 5:42 PM
Hello Sowjanya,
Thank you for your reply.
I could able to sort and delete the duplicates. but the point is how display that how many duplicates were present. I am trying to get the count of duplicates.
Thank you,
Best Regards,
Subramanyeshwer
08-24-2006 4:56 PM
this will give u the no. of duplicates of table t_t with fields f1, f2.
loop at t_t.
at new f2.
v1 = sy-tabix.
endat.
at end of f2.
v2 = sy-tabix.
read table t_t index v2.
v_c = v2 - v1 + 1.
write:/ t_t-f1, space, t_t-f2, v_c.
endat.
endloop.
then delete duplicates.
-Anu
08-24-2006 5:44 PM
Hello Anu,
Thank you for the reply.
Could you please eloborate a bit.
I could not understand the varialbles and flow of the code.
Thank you,
Best Regards,
Subramanyeshwer
08-24-2006 5:56 PM
Hi,
Rich's code is correct...
In debugging, You can check-out if all the individual fields are having the same values in itab1 and itab2 for the counter to get incremented.
Regards,
Raj
08-25-2006 11:24 AM
Hello Raj,
your reply forced me to check the program perfectly.
Thank you for your reply.
I checked the radio button "Very helpfull answer".
Regards,
Subramanyeshwer