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

loop on table with same field

Former Member
0 Likes
2,370

HI,

i have internal table and i want to now if i have the same field in table twice ,

i want to insert some code ,what is the best way to do that?

e.g.

itab
field1    field2
a                 1    
b               10
c               10 
d                 5
e                 6

i want to loop on itab and if in field2 i have 10 more then one time,

insert some code.

Best Regards

1 ACCEPTED SOLUTION
Read only

Clemenss
Active Contributor
0 Likes
1,819

Hi Michael,

the solution I proposed will not change anything with the internal table you want to check.

Your question was how to detect non-unique values in a certain table field. In your example, how to detect the second occurence of '10' in field 'field2'.

The solution is to create a second internal table just for the values of this field. This new table is good for nothing except the detection of non-unique entries - just what you want.

Please have a look at my solution again. ITAB is the internal table you have the values in. It is not modified at all - not the structure, not the contents. I will just LOOP at it to see the contents. I use 'ASSIGNING <field-symbol> because this makes the loop up to 6 times faster. In the loop, I put the value of 'field2' into the second, hashed table. If this is successful, it is the first occurence of this value.- If not, you have a duplicate.

Just copy and paste my code - it will work for you.

Regards,

Clemens

9 REPLIES 9
Read only

Former Member
0 Likes
1,819

use Some sort of logic like below,

Sort itab by field2.
loop at itab.
if temp = itab-field2.
inset code.
endif.
temp = itab-field2.
endloop.

Read only

MarcinPciak
Active Contributor
0 Likes
1,819

Hi Michael,

Go for control loop statements


data: pos type i.

Loop at itab.
  at new field2.
    pos = sy-tabix.  "rememebr position of first occurence of field2 value
  endat.

  at end of field2.
    if pos ne sy-tabix.   "check if first occurence is the last one
       "value for field2 is place more than one time in itab
       "do your coding here
    endif.
   clear pos.
  endat.
endloop.

Please note that field1 and field2 must be declared as key fields.

Regards

Marcin

Read only

0 Likes
1,819

Hi Clemens ,

Thanks,

the internal table is table that i don't think that i can change the key ,

the table is table extract from maintenance view and i move it to internal table and their i do manipulation.

Best Regards

Read only

Clemenss
Active Contributor
0 Likes
1,819

Hi Michael,

for this purpose a hashed table is useful:


field-symbols:
* use a field-symbol for optimal loop performance regardless of itab structure
  <line> like line of itab.
data:
  lt_field2_unique like hashed table of itab-field2 with unique key table line.
loop at itab assigning <line>.
* put field2 value into internal table - HASHED guarantees only unique entries
  insert <line>-field2 into table lt_field2_unique.
  if sy-subrc NE 0.
* duplicate insert fails - Duplicate detected - insert your code here
  endif. 
endloop.

Regards and Happy New Year!

Clemens

Read only

Former Member
0 Likes
1,819

Hi Clemens ,

Thanks,

the internal table is table that i don't think that i can use like hash,

the table is table extract from maintenance view and i move it to my internal table

and their i do the manipulation.

Best Regards

And Happy New Year

Read only

0 Likes
1,819

So you can declare your internal table with key fields different from maintenance view.


types: begin of t_tab,
            field1 type ...
            field2 type ...
          end of t_tab.

data: itab table table of t_tab with key field1 field2 with header line.

"now just extract data from your view to your internal table, I think only field names are critical here so you can use (in a loop)

move-corresponding view_structure to itab.

Try it out.

Marcin

Read only

Former Member
0 Likes
1,819

HI Marcin,

i try it and let u now .

Regards

Read only

Former Member
0 Likes
1,819

Hi,

Try like this:

data: begin of itab occurs 0,

field1 type c,

field2(10) type c,

end of itab.

data: c1 like itab-field2.

itab-field1 = 'a'.

itab-field2 = '1'.

append itab.

itab-field1 = 'b'.

itab-field2 = '10'.

append itab.

itab-field1 = 'c'.

itab-field2 = '10'.

append itab.

itab-field1 = 'd'.

itab-field2 = '5'.

append itab.

itab-field1 = 'e'.

itab-field2 = '6'.

append itab.

loop at itab.

if c1 <> itab-field2.

write:/ itab-field1, itab-field2.

else.

itab-field2 = 'duplicate'.

write:/ itab-field1, itab-field2.

endif.

c1 = itab-field2.

endloop.

Regards,

Bhaskar

Read only

Clemenss
Active Contributor
0 Likes
1,820

Hi Michael,

the solution I proposed will not change anything with the internal table you want to check.

Your question was how to detect non-unique values in a certain table field. In your example, how to detect the second occurence of '10' in field 'field2'.

The solution is to create a second internal table just for the values of this field. This new table is good for nothing except the detection of non-unique entries - just what you want.

Please have a look at my solution again. ITAB is the internal table you have the values in. It is not modified at all - not the structure, not the contents. I will just LOOP at it to see the contents. I use 'ASSIGNING <field-symbol> because this makes the loop up to 6 times faster. In the loop, I put the value of 'field2' into the second, hashed table. If this is successful, it is the first occurence of this value.- If not, you have a duplicate.

Just copy and paste my code - it will work for you.

Regards,

Clemens