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

Comparing 2 internal tables

former_member147217
Participant
0 Likes
1,875

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.

11 REPLIES 11
Read only

arindam_m
Active Contributor
0 Likes
1,825

Hi,

I think a LOOP on ITAB with a READ inside it on ITAB1 would help in doing this check easily.

Cheers,

Arindam

Read only

Former Member
0 Likes
1,825

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...

Read only

Former Member
0 Likes
1,825

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.

Read only

Former Member
0 Likes
1,825

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.

Read only

Former Member
0 Likes
1,825

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

Read only

Former Member
0 Likes
1,825

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 .

Read only

Former Member
0 Likes
1,825

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.

Read only

former_member209120
Active Contributor
0 Likes
1,825

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.


Read only

Arun_Prabhu_K
Active Contributor
0 Likes
1,825

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.

Read only

Former Member
0 Likes
1,825

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

Read only

soumik_de2
Participant
0 Likes
1,825

Hi

"write your logic.

"write your logic.