Skip to Content

Delete Duplicates from internal table with object references

Hi

How can I delete duplicates from an internal table in ABAP OO based on the value of one of the attributes?

I have created a method, with the following code:

......
  LOOP AT me->business_document_lines INTO l_add_line.

    CREATE OBJECT ot_line_owner
      EXPORTING
        i_user      = l_add_line->add_line_data-line_owner
        i_busdoc = me->business_document_id.

      APPEND ot_line_owner TO e_line_owners.

  ENDLOOP.
.....

e_line_owners are defined as a table containing only object references.

One of the attribute of the object in the table is called USER. And I would like to do a "delete ADJACENT DUPLICATES FROM e_line_owners", based on that attribute.

How can do this?

Regards,

Morten Nielsen

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

3 Answers

  • Best Answer
    author's profile photo
    Former Member
    Posted on Aug 21, 2007 at 07:06 PM

    Hello Morten

    Assuming that the instance attribute is <b>public </b>you could try to use the following coding:

      SORT e_line_owners BY table_line->user.
      DELETE ADJACENT DUPLICATES FROM e_line_owners
        COMPARING table_line->user.

    However, I am not really sure (cannot test myself) whether <b>TABLE_LINE</b> can be used together with SORT and DELETE.

    Alternative solution:

      DATA:
         ld_idx    TYPE sy-tabix.
    
      LOOP AT e_line_owners INTO ls_line.
        ld_idx = syst-tabix + 1.
    
        LOOP AT e_line_owners TRANSPORTING NO FIELDS FROM ld_idx
                       WHERE ( table_line->user = ls_line->user ).
          DELETE e_line_owners INDEX syst-tabix.
        ENDLOOP.
    
      ENDLOOP.

    Regards

    Uwe

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Morten Hjorth Nielsen

      Hello Morten

      Thanks for this feedback. I have seen and used TABLE_LINE together with READ TABLE and LOOP AT yet it is good to know that it works for the other statements as well.

      Best Regards

      Uwe

  • author's profile photo
    Former Member
    Posted on Aug 21, 2007 at 04:09 PM

    Hello,

    I think you will have to do it manually...

    Store the value of the attrib of the first line of the table in a temp value lv_attrib_old.

    LOOP starting a the second line INTO lv_line_new.

    check if the value of the attrib in the same.

    In this case delete the line.

    else

    lv_attrib_old = lv_line_new->attrib.

    ENDLOOP.

    Hope this help...

    Or you can do it when you create your table so you don't add it when the attribute has the same value as the old one.

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo
    Former Member
    Posted on Aug 21, 2007 at 04:17 PM

    Hello

    What about creating your internal table with index....so you wont be able to repeat elements inside the IT

    Bye

    Gabriel

    Add comment
    10|10000 characters needed characters exceeded