2013 Aug 31 6:49 AM
hi everyone,
i have a query,
i have 2 internal tables
data: begin of itab,
col1 type i,
col2 type i,
end of itab,
begin of itab1,
col1 type i,
col2 type i,
end of itab1.
now i have values in both fields of the internal table . now i want to compare itab-col1 = itab1-col1 and itab-col2 = itab1-col2.
and if the values of each record is equal then +1 nd if not equal then -1.
can anyone tel me how to do this.
2013 Aug 31 12:59 PM
Hi,
I think a LOOP on ITAB with a READ inside it on ITAB1 would help in doing this check easily.
Cheers,
Arindam
2013 Aug 31 1:24 PM
just use
if itab1 gt itab.
" if itab's is bigger in case of no of record and if no. of record is same of all then data wil be compared.
endif.
gt(greater then).
also u can use.. lt(less then) , ge(greater then equal)
.. Chandra...
P.S.: if you don't want to use then you have to use Loop and inside it read table...
2013 Aug 31 1:45 PM
you can use the table body operator to compare the contents of two internal tables.
If it1[] = it2[]
The internal tables must have the same structure, if they are different then compare them manually row by row.
for row by row comparison
first sort bot the internal tables based on the comparing fields.
take the largest count table
then loop through the it1 and inside that loop through it2 and check for the field comparisons.
2013 Aug 31 4:11 PM
Hi,
Why you have defined 2 structures(itab & itab1) of same type ?
I think it should be like
TYPES:
begin of s_itab,
col1 type i,
col2 type i,
end of s_itab,
DATA: itab type standard table of s_itab.
wa_itab type s_itab.
itab1 type standard table of s_itab.
wa_itab1 type s_itab.
SORT itab BY col1 col2. " if neccesary
SORT itab1 BY col1 col2. " if neccesary
Loop at itab into wa_itab.
Read table itab1 into wa_itab1.
if wa_itab-col1= wa_itab1-col1.
" desired logic
else.
" desired logic
endif.
if wa_itab-col2= wa_itab1-col2.
" desired logic
else.
" desired logic
endif.
endloop.
please correct me if I'm wrong.
Harshawardhan.
2013 Aug 31 9:24 PM
HI,
try below code
Types: being of ty_itab,
col1 type i,
col2 type i,
end of ty_itab.
data: itab type table of ty_itab,
wa type ty_itab,
itab1 type table of ty_itab,
wa1 type ty_itab.
-----
Fetch data in both internal tables
loop at itab into wa.
read table itab1 into wa1.
if sy-subrc = 0.
if wa-col1 = wa1-col1.
write your logic......
endif.
if wa-col2 = wa1-col2.
write your logic....
endif.
endif.
clear: wa, wa1.
endloop.
---
Jack
2013 Sep 01 9:45 AM
data: begin of itab,
col1 type i,
col2 type i,
end of itab,
begin of itab1,
col1 type i,
col2 type i,
end of itab1.
data:
t_itab like table of itab ,
t_itab1 like table of itab1 ,
w_itab like itab,
w_itab1 like itab .
loop at t_itab into w_tab .
read table t_itab1 into w_tab1 index sy-tabix .
if sy-subrc = 0 .
if w_tab-col1= w_tab1-col1 .
"write the logic "
endif.
if w_tab-col2 = w_tab1-col2 .
"write your logic "
endif .
endif .
endloop .
2013 Sep 01 1:25 PM
Hi,
"When ever we compare two table value you can loop for one table and use read statement for another table before going for this you should sort the table. As you know binary search is faster than others.
Sort: t_itab1, t_itab2 by Col1.
loop at t_itab into w_tab .
read table t_itab1 into w_tab1 with key wa_tab1-col1 = wa_tab-col1 binary search.
if sy-subrc = 0 .
if w_tab-col2 = w_tab1-col2 .
"write the logic " i.e Add the value 1.
else.
"write the logic " i.e Subtract the value 1.
endif.
endif .
endloop .
Hope this will helpful to you.
Regards,
John.
2013 Sep 02 3:59 AM
Hi Disha Shah,
Simple .. try like this...
TYPES : BEGIN OF ty_itab,
col1 TYPE i,
col2 TYPE i,
END OF ty_itab.
DATA : it_itab TYPE TABLE OF ty_itab,
wa_itab TYPE ty_itab,
it_itab1 TYPE TABLE OF ty_itab,
wa_itab1 TYPE ty_itab.
wa_itab-col1 = '1'.
wa_itab-col2 = '2'.
APPEND wa_itab TO it_itab.
CLEAR wa_itab.
wa_itab-col1 = '1'.
wa_itab-col2 = '5'.
APPEND wa_itab TO it_itab.
CLEAR wa_itab.
wa_itab1-col1 = '1'.
wa_itab1-col2 = '2'.
APPEND wa_itab1 TO it_itab1.
CLEAR wa_itab1.
wa_itab1-col1 = '1'.
wa_itab1-col2 = '1'.
APPEND wa_itab1 TO it_itab1.
CLEAR wa_itab1.
sort : it_itab by col1,
it_itab1 by col1.
Loop at it_itab into wa_itab.
read TABLE it_itab1 into wa_itab1 with key col1 = wa_itab-col1 col2 = wa_itab-col2 BINARY SEARCH.
if sy-subrc = 0.
write : / '+1'.
else.
write : / '-1'.
endif.
endloop.
2013 Sep 02 4:39 AM
Hello Disha Shah.
I would recommend to use field symbols for faster results (WHEN INTERNAL TABLE RECORDS ARE MORE).
TYPES : begin of ttab,
col1 type i,
col2 type i,
TYPES : end of ttab,
DATA : itab type table of ttab with header line,
itab1 type table of ttab with header line.
FIELD-SYMBOLS : <fs> type ttab.
"Assuming that both tables have equal number of records
SORT : itab,itab1.
loop at itab assigning <fs>.
read table itab1 with key col1 = <fs>-col1 col2 = <fs>-col2 BINARY SEARCH.
if sy-subrc = 0.
write: '+1'. "Your business logic
else.
write: '-1'. "Your business logic
endif.
endloop.
Regards.
2013 Sep 02 5:16 AM
Hi Disha,
LOOP AT int_tab1 into wa_str1.
READ TABLE int_tab2 WITH KEY int_tab1-field1 EQ wa_str1-field1
int_tab1-field2 EQ wa_str1-field2
int_tab1-field3 EQ wa_str1-field3
TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
Message " Same" Type 'S'.
ENDIF.
ENDLOOP.
Hope this helps.
Regards
D Amarnath
2013 Sep 02 7:31 AM
Hi Disha Shah ....
first of all your question isn't clear ......... you haven't mentioned that whether both internal table is having same number of records or not......... let me assumed that they are having same no of record . i am also not clear what logic you want to put (Where + 1 or - 1 ).
loop at itab1 into watab1.
read table itab2 into watab2 with key col1 = watab1-col1
col2 = watab1-col2.
if sy-subrc eq 0.
"write your logic.
else.
"write your logic.
endif.
endloop.