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

ITAB - look for duplicates

Former Member
0 Likes
954

I want to do something like this....

Let's say my ITAB has MATNR & DUP fields & let's say ITAB has entries like this...

ITAB-MATNR      ITAB-DUP

123
123
345
456

if any of the MATNR's are repeated in ITAB I want to flag DUP for such repeated records so the ITAB would look like below......

ITAB-MATNR      ITAB-DUP

123
123                    X
345
456

Please note : only the records repeated after the first unique record should be flagged.

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
925

Try this

REPORT  zrich_0001.

TYPE-POOLS: abap.

TYPES: BEGIN OF ttab,
        matnr(18) TYPE c,
        dup TYPE abap_bool,
       END OF ttab.

DATA: itab TYPE TABLE OF ttab.
DATA: wa LIKE LINE OF itab.

DATA: itab_copy TYPE TABLE OF ttab.
DATA: wa_copy LIKE LINE OF itab_copy.

DATA: tabix TYPE i.


wa-matnr = '000000000000000123'.  APPEND wa TO itab.
wa-matnr = '000000000000000123'.  APPEND wa TO itab.
wa-matnr = '000000000000000123'.  APPEND wa TO itab.
wa-matnr = '000000000000000345'.  APPEND wa TO itab.
wa-matnr = '000000000000000456'.  APPEND wa TO itab.
wa-matnr = '000000000000000456'.  APPEND wa TO itab.


itab_copy = itab.

LOOP AT itab_copy INTO wa_copy.

  tabix = sy-tabix + 1.

  LOOP AT itab INTO wa FROM tabix
             WHERE matnr = wa_copy-matnr.
    wa-dup = abap_true.
    MODIFY itab FROM wa.
  ENDLOOP.

ENDLOOP.


LOOP AT itab INTO wa.
  WRITE:/ wa-matnr, wa-dup.
ENDLOOP.

Regards,

Rich Heilman

I want to do something like this....

Let's say my ITAB has MATNR & DUP fields & let's say ITAB has entries like this...

ITAB-MATNR      ITAB-DUP

123
123
345
456

if any of the MATNR's are repeated in ITAB I want to flag DUP for such repeated records so the ITAB would look like below......

ITAB-MATNR      ITAB-DUP

123
123                    X
345
456

Please note : only the records repeated after the first unique record should be flagged.

7 REPLIES 7
Read only

Former Member
0 Likes
925
loop at itab.
if itab-matnr = v_matnr.
itab-dup = 'X'.
endif.
v_matnr = itab-matnr.
modify itab index sy-tabix.
endloop.
Read only

0 Likes
925

Sort itab by matnr.

loop at itab.

if sy-tabix eq 1.

v_matnr = itab-matnr.

else

if itab-matnr = v_matnr.

itab-dup = 'X'.

endif.

v_matnr = itab-matnr.

modify itab index sy-tabix.

endif.

endloop.

Just some additional inputs for Ravis logic

Cheers

Kothand

Read only

Former Member
0 Likes
925

sort itab by matnr.

loop at itab.
at new matnr.
clear counter.
end at.

counter = counter + 1.


if counter gt 1.
itab - dup = X.
modify itab.
endif.
endloop.

Read only

Former Member
0 Likes
925

Hi,

Try this..

SORT itab BY matnr.

data: v_prev_matnr type matnr.
field-symbols: <wa> LIKE LINE OF ITAB.

LOOP AT ITAB ASSIGNGING <WA>.

IF sy-tabix = 1.
  v_prev_matnr = <wa>-matnr.
ENDIF.

if v_prev_matnr ne <wa>-matnr.
  clear: v_count.
endif.

v_count = v_count + 1.

* Set the duplicate flag..if there is more than one record.
if v_count > 1.
  <wa>-dup = 'X'.
endif.

v_prev_matnr = <wa>-matnr.

ENDLOOP.

Hope this helps.

Thanks

Naren

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
926

Try this

REPORT  zrich_0001.

TYPE-POOLS: abap.

TYPES: BEGIN OF ttab,
        matnr(18) TYPE c,
        dup TYPE abap_bool,
       END OF ttab.

DATA: itab TYPE TABLE OF ttab.
DATA: wa LIKE LINE OF itab.

DATA: itab_copy TYPE TABLE OF ttab.
DATA: wa_copy LIKE LINE OF itab_copy.

DATA: tabix TYPE i.


wa-matnr = '000000000000000123'.  APPEND wa TO itab.
wa-matnr = '000000000000000123'.  APPEND wa TO itab.
wa-matnr = '000000000000000123'.  APPEND wa TO itab.
wa-matnr = '000000000000000345'.  APPEND wa TO itab.
wa-matnr = '000000000000000456'.  APPEND wa TO itab.
wa-matnr = '000000000000000456'.  APPEND wa TO itab.


itab_copy = itab.

LOOP AT itab_copy INTO wa_copy.

  tabix = sy-tabix + 1.

  LOOP AT itab INTO wa FROM tabix
             WHERE matnr = wa_copy-matnr.
    wa-dup = abap_true.
    MODIFY itab FROM wa.
  ENDLOOP.

ENDLOOP.


LOOP AT itab INTO wa.
  WRITE:/ wa-matnr, wa-dup.
ENDLOOP.

Regards,

Rich Heilman

Read only

0 Likes
925

Full points to Rich as I need not sort the ITAB to get this thing working.....

Read only

0 Likes
925

Raju....

You did not tell us that you did not wanted to sort the itab..:(...

add a clear statement after my code to make it work..I cannot edit it now...

Just kidding....:)

Edited by: Ravi Kanth Talagana on Jul 11, 2008 7:40 PM