2013 Aug 27 5:09 PM
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
2013 Aug 28 6:18 AM
2013 Aug 27 8:10 PM
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.
2013 Aug 28 5:41 AM
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.
2013 Aug 28 5:54 AM
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
2013 Aug 28 6:18 AM
2013 Aug 28 6:32 AM
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
2013 Aug 28 6:45 AM
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
2013 Aug 28 7:06 AM
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..
2013 Aug 28 7:55 AM
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.
2013 Aug 28 10:33 AM
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |