Application Development and Automation 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: 
Read only

copy ADJACENT DUPLICATES

Former Member
0 Likes
1,629

Hi All,

How to copy the duplicates from sorted internal table values into other internal table.

Thanks,

Venkat

Hi All,

How to copy the duplicates from sorted internal table values into other internal table.

Thanks,

Venkat

9 REPLIES 9
Read only

Former Member
0 Likes
1,081
loop at itab.
  clear flag.
  at new matnr.
    flag = 'X'.
  endat.
  if flag ne 'X'.
  move-corresponding itab to itab1.
  append itab1.
  endif.
  
endloop.

where matnr is the duplicate entry in itab.

Message was edited by: Chandrasekhar Jagarlamudi

Read only

Former Member
0 Likes
1,081

Hi,

I think there is no direct command. You will have to build ur own logic.

>> First take a copy of the original internal table

>> Then SORT the table and do a adjacent duplicate

>> Now you have 2 tables one with original data and second with unique data.

>> Compare the two tables using a loop.

itab_1[] = itab[].

SORT ITAB bY F1 F2.

DELETE ADJACENT DUPLICATES FROM ITAB.

Loop at itab_1.

READ TABLE ITAB with key ITAB = itab_1.

IF sy-subrc eq 0.

CONTINUE.

ELSE.

DELETE.

ENDIF.

endloop.

Cheers

VJ

Message was edited by: Vijayendra Rao

Read only

0 Likes
1,081

Do you have any example code.

Thanks,

Venkat

Read only

0 Likes
1,081

Hello Vijayendra,

READ TABLE ITAB with key ITAB = itab_1.

This stmt is not working.

Thanks,

Venkat

Read only

0 Likes
1,081

change it to

READ TABLE ITAB with key ITAB-fld1 = itab_1-fld1
                         ITAB-fld2 = itab_1-fld2. 

~Suresh

Read only

0 Likes
1,081

You can write the fields with key statement..eg

READ TABLE ITAB with KEY itab-f1 = itab_1-f1

itab-f2 = itab_1-f2....

etc

Regards

anurag

Read only

Former Member
0 Likes
1,081

I dont think it can done via a single command..need to use the below logic.

sort itab.

loop at itab.

istab = itab.

at end of field.

move-corresponding istab to itab1.

append itab1.

endat.

endloop.

Regards

Anurag

Read only

Former Member
0 Likes
1,081

Hi,

try this code:

sort itab.

loop at itab.

at new fld1.

..

endat.

itabdup-fld1 = itab-fld1.

append itabdup.

clear itabdup.

end loop.

delete adjacent duplicates from itab comparing fld1.

reward if helpful,

keerthi.

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,081

Please see the following example. this program pulls out records only when they have a duplicate entry in the table. In this case, the record with GHI will be outputed.



report zrich_0001.


data: begin of itab occurs 0,
      f1 type c,
      f2 type c,
      f3 type c,
      end of itab.
data: begin of itabx occurs 0,
      f1 type c,
      f2 type c,
      f3 type c,
      end of itabx.
data: begin of itabo occurs 0,
      f1 type c,
      f2 type c,
      f3 type c,
      end of itabo.

data: counter type i.


itab = 'ABC'.  append itab.
itab = 'DEF'.  append itab.
itab = 'GHI'.  append itab.
itab = '123'.  append itab.
itab = '456'.  append itab.
itab = '789'.  append itab.
itab = 'GHI'.  append itab.


itabx[] = itab[].

sort itabx ascending.
sort itab ascending.
delete adjacent duplicates from itab.

loop at itab.

  clear counter.
  loop at itabx where f1 = itab-f1
                  and f2 = itab-f2
                  and f3 = itab-f3.
    counter = counter + 1.
  endloop.
  if counter > 1.
    move itab to itabo.
    append itabo.
  endif.

endloop.


* Output the record that had a duplicate entry
loop at itabo.
  write:/ itabo.
endloop.

Regards,

Rich Heilman