02-28-2007 10:37 PM
Hello friends,
I have an Internal table and it is getting some duplicate values. For example,
I have the same values in line 3, 6 and 9 of the internal table ( All fields have the same value)
I want to delete Line 6 and 9 from the internal table. However i dont want to sort the Internal table.
Any suggestions,
Madhu.
02-28-2007 10:49 PM
Hi,
Check this..
DATA: BEGIN OF ITAB OCCURS 0,
VALUE,
END OF ITAB.
DATA: V_INT TYPE SYTABIX.
ITAB-VALUE = '1'. APPEND ITAB.
ITAB-VALUE = '2'. APPEND ITAB.
ITAB-VALUE = '3'. APPEND ITAB.
ITAB-VALUE = '1'. APPEND ITAB.
ITAB-VALUE = '4'. APPEND ITAB.
ITAB-VALUE = '3'. APPEND ITAB.
LOOP AT ITAB.
CLEAR: V_INT.
LOOP AT ITAB WHERE VALUE = ITAB-VALUE.
V_INT = V_INT + 1.
IF V_INT > 1.
DELETE ITAB.
ENDIF.
ENDLOOP.
ENDLOOP.
Thanks,
Naren
02-28-2007 10:39 PM
Hi,
You can use index number to delete rows.
DELETE itab INDEX sy-tabix.
Regards,
Amit
02-28-2007 10:41 PM
02-28-2007 10:43 PM
how can i dynamically figure the index number.
Any example please.
Madhu
02-28-2007 10:45 PM
Thats a tricky thing as you you can check out which is a duplicate row in Itab just store the index number of that row in a variable and pass it to delete.
But this require a very dynamic and tricky programming, thats why it is prefered to sort and delete. this even save a lot on performance of the report too.
Regards,
Amit
02-28-2007 10:45 PM
Use this:
data: lv_index like sy-index.
loop at itab.
lv_index = sy-index.
DELETE itab INDEX lv_index.
endloop.
Thanks,
Santosh
02-28-2007 10:50 PM
You have to process the ITAB for each 'n every duplicate value.
e.g.
LOOP at ITAB into wa_itab1.
loop at itab into wa_itab2.
Check wa_itab1 = wa_itab2.
DELETE ITAB from wa_itab1.
ENDLOOP.
ENDLOOP.
it miet bthe ogic. just try it ou.
by d way y don't want to SORT the ITAB.
this can be asilydone using SORT 'n DELTE ADAJCENT
hope this will help
Ashwani
02-28-2007 10:56 PM
Thanks All,
I will try the logic suggested.
However the order is very important here. Its basically the IDOC segments, So I cant sort. One more important thing is when I am deleting the ITAB i cant delete the first hit record. As i said in my post from line 3, 6 and 9 i cant delete 3.
THanks,
Madhu.
02-28-2007 10:49 PM
Hi,
Eg. Itab has your internal table
itab1[] = itab[].
sort itab1 by <your field name >
delete adjacent duplicates from itab1 comparing <your field name>
loop at itab1.
read table itab with key <field Name > = itab1-<fieldname>
if sy-subrc eq 0
delete itab index sy-tabix.
endif.
endloop.
After this loop itab contain the records as you mentioned.
ie Every first entry in itab of the key has been deleted.
aRs
02-28-2007 10:49 PM
Hi,
Check this..
DATA: BEGIN OF ITAB OCCURS 0,
VALUE,
END OF ITAB.
DATA: V_INT TYPE SYTABIX.
ITAB-VALUE = '1'. APPEND ITAB.
ITAB-VALUE = '2'. APPEND ITAB.
ITAB-VALUE = '3'. APPEND ITAB.
ITAB-VALUE = '1'. APPEND ITAB.
ITAB-VALUE = '4'. APPEND ITAB.
ITAB-VALUE = '3'. APPEND ITAB.
LOOP AT ITAB.
CLEAR: V_INT.
LOOP AT ITAB WHERE VALUE = ITAB-VALUE.
V_INT = V_INT + 1.
IF V_INT > 1.
DELETE ITAB.
ENDIF.
ENDLOOP.
ENDLOOP.
Thanks,
Naren
02-28-2007 11:04 PM
hi
You can delete the multiple records from database table by putting all the records which u want to delete in internal table.
DELETE SMARA FROM TABLE ITAB.
in this case whatever you have in internal table will be deleted from SMARA.
append the internal table.
else try this :
SORT itab ASCENDING BY xxx.
DELETE ADJACENT DUPLICATES FROM itab COMPARING xx.
else : You can solve your prob using below code:
DATA: ind(1), p_val like itab-field1.
SORT itab ASCENDING field1.
LOOP AT itab INTO wa_itab.
CLEAR: p_val, ind.
ON CHANGE OF field1.
p_val = itab-field1.
ind = 'X'.
ENDON.
IF itab-field1 = p_val AND NOT ind IS INITIAL.
DELETE itab INDEX sy-tabix.
ENDIF.
ENDLOOP.
sri
award points if helpful
02-28-2007 11:11 PM
02-28-2007 11:25 PM