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

Delete Duplicates from internal table with object references

morten_nielsen
Active Contributor
0 Likes
1,095

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

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
861

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

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

5 REPLIES 5
Read only

Former Member
0 Likes
861

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.

Read only

Former Member
0 Likes
861

Hello

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

Bye

Gabriel

Read only

uwe_schieferstein
Active Contributor
0 Likes
862

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

Read only

0 Likes
861

Thanks Uwe !

The first suggestion worked like a dream

Regards

Morten

Read only

0 Likes
861

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