Application Development 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: 

Compare and replace.

Former Member
0 Kudos

hello all,

I have an internal table with the following output.

A B

1 2

3 4

5 6

2 7

7 10

Now i have to compare these two columns.

ex: for field B the first record is 2 we have to check whether 2 is present in A .

we can see 2 is present in A now for that entry the related entry in B is 7.Again we have to check whether 7 is present in A .we can see 7 is there in A for this entry the related entry is 10.

like this we have to compare for the whole table .

Here 10 is the last entry now we have to replace as,

A B

1 10

3 4

5 6

2 10

7 10

Regards,

Vaasu.

4 REPLIES 4

Former Member
0 Kudos

Hi,

Try this logic,

data bvalue type itab-b.

data bvalue type itab-b.

data tabix type sy-tabix.

data flag type c.

Loop at itab.

bvalue = itab-b.

tabix = sy-tabix.

do.

read table itab into wa_itab with key a = bvalue.

if sy-subrc eq 0.

bvalue = wa_itab-b.

flag = 'X'.

ENDIF.

ELSE.

exit.

enddo.

if flag eq 'X'.

itab-b = bvalue.

modify itab index tabix.

endif.

endloop.

Former Member
0 Kudos
i = 2.   "Store the initial value to be checked in column B
DO.
   READ TABLE itab INTO wa_itab WITH KEY b = i.
   IF SY-SUBRC = 0. "If value found in Column B
       wa_index-index = sy-tabix.
       append wa_index to it_bindex.   "Store the index of Row in another internal table
       clear wa_index.
       j = wa_itab-a. "Set the value to be checked in Column A
      READ TABLE itab INTO wa_itab1 WITH KEY a = j. "Read table for checking Column A
      IF SY-SUBRC = 0. " If value found in column A
         wa_index-index = sy-tabix.
         append wa_index to it_aindex. " Store the index in another table for future use
         clear wa_index.
          i = wa_itab1-b. 'Change value to be checked in Column B
      ELSE.
          a_flag = 'X'. "If Value not found in Column A set the flag and Exit
          EXIT.                  
      ENDIF.
   ELSE.
     b_flag = 'X'. "If Value not found in Column B set the flag and Exit
     EXIT.
   ENDIF.
ENDDO.

IF a_flag = 'X'. "If check failed in A column, change the latest B column value in all rows identified earlier
   LOOP AT it_bindex INTO wa_index.
      wa_itab-b = j.
      MODIFY itab FROM wa_itab INDEX wa_index-index.
   ENDLOOP.
ELSE. "If check failed in B column, change the latest A column value in all rows identified earlier
  LOOP AT it_aindex INTO wa_index.
      wa_itab-a = i.
      MODIFY itab FROM wa_itab INDEX wa_index-index.
   ENDLOOP.
ENDIF.

Hope this is clear.

Thanks,

Lakshmi

Former Member
0 Kudos

Hi Vaasu,

The following snippet will work for your requirement.

Here it_tab is your table with fields A and B,

wa_tab,wa_tab1 work area for the table,

ty_tab structure of tabe it_tab.

Declare variable for holding B value like this

DATA: w_hold TYPE ty_tab-b.

then implement the following logic,

LOOP AT it_tab INTO wa_tab.
* Copying current B value to a variable
  w_hold = wa_tab-b.

  DO.
    READ TABLE it_tab INTO wa_tab1 WITH KEY a = w_hold .
    IF sy-subrc = 0.
* If entry exists change w_hold vale to current B value
      w_hold = wa_tab1-b.
    ELSE.
* If no entry exists come out of the loop
      EXIT.
    ENDIF.
  ENDDO.

* If w_hold value is not the initial B value then assign w_hold to B
  IF w_hold NE wa_tab-b.
    wa_tab-b = w_hold.
    MODIFY it_tab FROM wa_tab.
  ENDIF.
ENDLOOP.

Hope this will help you.

Regards,

Manoj Kumar P

Former Member
0 Kudos

TYPES : BEGIN of type,

a(2) TYPE c,

b(2) TYPE c,

END OF type.

DATA : itab TYPE TABLE OF type WITH HEADER LINE.

DATA : tabix TYPE sy-tabix,

wa_b(2) TYPE c.

itab-a = 1. itab-b = 2. APPEND itab.

itab-a = 3. itab-b = 4. APPEND itab.

itab-a = 5. itab-b = 6. APPEND itab.

itab-a = 2. itab-b = 7. APPEND itab.

itab-a = 7. itab-b = 10. APPEND itab.

LOOP AT itab.

wa_b = itab-b.

tabix = sy-tabix.

do.

READ TABLE itab WITH KEY a = wa_b.

IF sy-subrc = 0.

wa_b = itab-b.

continue.

ELSE.

MODIFY itab INDEX tabix TRANSPORTING b .

EXIT.

ENDIF.

enddo.

ENDLOOP.

LOOP AT itab.

write: / itab-a, '--',itab-b.

ENDLOOP.

This piece of code will work for you.Specifically for ur example u can test.