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

Change value in a table

Former Member
0 Likes
3,987

Hi experts,

How can I do this with an efficient ABAP code?

-I have to check the data contained in a column 1

-If I find values on column 1 staring with number 7 and ended with a letter (e.g 71233444B)

-Then I have to transform the value contained in column 2 by replacing it with the following conditions:

            -Take the value in column 1

            -Replace 7 by a ZERO

            -Replace the letter by à Zero

            -Add ‘01’ at the end

Below an example:

Actual values in table

Column 1

Column2

71233444B

XXXXXX

712334443

YYYYY

Expected values after treatment

Column 1

Column2

71233444B

01233444001

712334443

YYYYY

Thanks for your support.

Amine

1 ACCEPTED SOLUTION
Read only

former_member220538
Active Participant
0 Likes
3,740
Hi Amine,
Here     t_itab is an Internal table with two columns col1 and col2.
              x_itab is the workarea.
              Str  -  string variable
              len  -  length  of string
 
LOOP AT T_ITAB INTO X_ITAB.
  str = x_itab-col1.
  len = STRLEN( str ) - 1.
  CHECK str(1) = '7' AND str+len(1) CO sy-abcde.
  REPLACE str+0(1) IN str WITH '0'.
  REPLACE str+len(1) IN str WITH '0'.
  CONCATENATE  str '01' INTO x_itab-col2.
  modify t_itab from x_itab.
ENDLOOP.
Now use the internal table t_itab as required..
Regards,
Jeffin
9 REPLIES 9
Read only

Former Member
0 Likes
3,740

Hi,

data str type string.

str = col1.

REPLACE '7' IN str WITH '0' .

replace all occurrence of ' [ a - z ]' in str with '0'.

concatenate str '01' into str.

Hope this will helpful to you.

Regards,

John.

Read only

Former Member
0 Likes
3,740

Hi Amine,

I have tried a simple list program for the scenario you mentioned and it worked. Please check the below code.

types : begin of ty_tab,

           col1 type string,

           col2 type string,

         end of ty_tab.

data : t_tab type standard table of ty_tab,

        x_tab type ty_tab,

        v_len type i,

        v_var(132) type c,

        v_alp type string value 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.

x_tab-col1 = '71233444B'.

x_tab-col2 = 'XXXXXX'.

append x_tab to t_tab.

clear x_tab.


x_tab-col1 = '712334443'.

x_tab-col2 = 'YYYYY'.

append x_tab to t_tab.

clear x_tab.

loop at t_tab into x_tab.

   write : / x_tab-col1, x_tab-col2.          "Actual Values

endloop.

loop at t_tab into x_tab.

   if x_tab-col1 ca '7'.

     v_len = strlen( x_tab-col1 ).

     v_len = v_len - 1.

     if x_tab-col1+v_len(1) ca v_alp.

       v_var = x_tab-col1.

       replace '7' in v_var with '0'.

       v_var+v_len(1) = '0'.

       concatenate v_var '01' into v_var.

       x_tab-col2 = v_var.

     endif.

   endif.

   write : / x_tab-col1 , x_tab-col2.     " Expected values after treatment.

endloop.

Result attached.

Regards,

Shahanaz Hussain.



Read only

former_member209120
Active Contributor
0 Likes
3,740

Hi amine lamksissi,

Try like this

TYPES : BEGIN OF ty_final,
         col1 TYPE string,
         col2 TYPE string,
         END OF ty_final.


DATA : it_final TYPE TABLE OF ty_final,
        wa_final TYPE ty_final.

DATA : htype TYPE dd01v-datatype,
        stng TYPE string,
        var1 TYPE i,
        var2 TYPE string,
        slen TYPE i,
        len TYPE i.

wa_final-col1 = '71233444B' .
wa_final-col2 = 'XXXXXX'.
APPEND wa_final TO it_final.
CLEAR wa_final.

wa_final-col1 = '712334443' .
wa_final-col2 = 'YYYYY'.
APPEND wa_final TO it_final.
CLEAR wa_final.


LOOP AT it_final INTO wa_final.

   slen = STRLEN( wa_final-col1 ).

   len = slen - 1.

   stng = wa_final-col1+len(1).


   CALL FUNCTION 'NUMERIC_CHECK'
     EXPORTING
       string_in        stng
    IMPORTING
*   STRING_OUT       =
      htype            = htype.

   var1 slen - 2.

   IF htype EQ 'CHAR'.
     CLEAR wa_final-col2.
     var2 = wa_final-col1+1(var1).
     CONCATENATE '0' var2 '001' INTO wa_final-col2.
     MODIFY it_final FROM wa_final TRANSPORTING col2.
     CLEAR wa_final.
   ENDIF.

ENDLOOP.

LOOP AT it_final INTO wa_final.
   WRITE : / wa_final-col1, wa_final-col2.
   CLEAR wa_final.
ENDLOOP.


Regards,

Ramesh.T

Read only

former_member220538
Active Participant
0 Likes
3,741
Hi Amine,
Here     t_itab is an Internal table with two columns col1 and col2.
              x_itab is the workarea.
              Str  -  string variable
              len  -  length  of string
 
LOOP AT T_ITAB INTO X_ITAB.
  str = x_itab-col1.
  len = STRLEN( str ) - 1.
  CHECK str(1) = '7' AND str+len(1) CO sy-abcde.
  REPLACE str+0(1) IN str WITH '0'.
  REPLACE str+len(1) IN str WITH '0'.
  CONCATENATE  str '01' INTO x_itab-col2.
  modify t_itab from x_itab.
ENDLOOP.
Now use the internal table t_itab as required..
Regards,
Jeffin
Read only

Former Member
0 Likes
3,740

Hi,

To read any particular column in a variable you can use offset reading, for example

wa-field1+0(1) will return you the first column.

To identify the last character in the string try the below logic.

DATA lv_length TYPE i.

lv_length = strlen( wa-field1 ).

lv_length = lv_length - 1.

lv_last_char = wa-field1+lv_length(1).

The easiest and most efficient way i foud to update an internal table is to use Field Symbols

For Eg.

LOOP AT lt_itab ASSIGNING <lfs_wa>.

ENDLOOP.

Since by using Field Symbols we are directly refering the memory location, which avoides the overhead of modification to the internal table.

Thanks,

Franio Kappen

Read only

Former Member
0 Likes
3,740

Hi Amine,

You can try this:

Suppose your internal table IT_DATA has two columns,declare an internal table of the same type as IT_DATA and name it IT_DATA_TEMP.

CONSTANTS : LC_NUM TYPE STRING VALUE '0123456789'.

IT_DATA_TEMP[] = IT_DATA[].

LOOP at IT_DATA_TEMP into WA_DATA.

    LV_LEN = STRLEN(WA_DATA-COLUMN1) - 1. " Getting the position of last character

    LV_LEN_STR = LV_LEN - 1.                            " Getting the second last position

" If the First Digit is 7 and the Last Digit doesnt contain a number

    IF WA_DATA-COLUMN1+0(1) = '7' AND WA_DATA-COLUMN1+LV_LEN(1) NA LC_NUM.

" Concatenate 0 , the remaining digits until second last, 0 and 01 into column2

        CLEAR WA_DATA-COLUMN2.

        CONCATENATE '0' WA_DATA-COLUMN1+1(LV_LEN_STR) '0' '01'

        INTO WA_DATA-COLUMN2.

        APPEND WA_DATA TO IT_DATA.

   ENDIF.

       CLEAR : WA_DATA.

ENDLOOP.

Hope this helps

Read only

0 Likes
3,740

Hi, Check the code below...

Data : len type i,

          idx LIKE sy-tabix.

loop at itab into wa_tab.

     idx = sy-tabix.

     MOVE wa_tab-col1 TO wa_tab-col2.

     len = lenght(wa_tab-col2).

     check wa_tab-col2(1) eq '7' .

     TRANSLATE wa_tab-col2+len(1) TO UPPER CASE.

     check wa_tab-col2+len(1) CA 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

     wa_tab-col2(1) = 0.

      wa_tab-col2+len(1) = 0.

     CONCTINATE wa_tab-col2 '01' TO wa_tab-col2.

      MODIFY  itab FROM wa_tab INDEX idx TRANSPORTING col2 .

endloop.

???helpfull ..points please..

Read only

chundru_ravindra
Participant
0 Likes
3,740

Hi,

you can do like this also,

data: len type i.

loop at itab into wa.

clear : len.

len = strlen( wa-col1 ).

len = len - 1.

if wa-col1+0(1) = '7' and wa-col1+len(1) ca 'A TO Z'.

len = len - 1.

concatenate '0' wa-col1+1(len) '001' into wa-col2.

modify itab from wa." Modify itab based on Index

endif.

endloop.

Read only

Former Member
0 Likes
3,740

Thanks to all of you guys.

Amine