‎2009 Jan 01 8:11 PM
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
‎2009 Jan 02 11:54 AM
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
‎2009 Jan 01 8:36 PM
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.
‎2009 Jan 01 8:37 PM
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
‎2009 Jan 01 9:07 PM
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
‎2009 Jan 01 8:57 PM
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
‎2009 Jan 01 9:09 PM
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
‎2009 Jan 01 9:16 PM
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
‎2009 Jan 02 7:48 AM
‎2009 Jan 02 8:41 AM
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
‎2009 Jan 02 11:54 AM
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