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: 

REPLACE/TRANSLATE --> Quick Points in a hurry!!

Former Member
0 Kudos
161

Hey there, in a hurry here!

I'm loading a file using ws_upload and in my record which is 1000 bytes long several elements are encapsulated in " ", I need to remove them. There is only one field in the record it_upfile2-csvdata. I can easily replace all the " and not leave spaces behind. I was using search/replace in a while/endwhile loop but it appears to be looping out of control...

CLEAR flg_r.

flg_r = 'X'.

WHILE flg_r = 'X'.

SEARCH it_upfile2-csvdata FOR '"'.

IF sy-subrc = 0.

flg_r = 'X'.

REPLACE '"' WITH '' INTO it_upfile2-csvdata.

ELSE.

CLEAR flg_r.

ENDIF.

ENDWHILE.

flg_r = 'X'.

WHILE flg_r = 'X'.

SEARCH it_upfile2-csvdata FOR ' , '.

IF sy-subrc = 0.

flg_r = 'X'.

REPLACE ' , ' WITH ',' INTO it_upfile2-csvdata.

ELSE.

CLEAR flg_r.

ENDIF.

ENDWHILE.

1 ACCEPTED SOLUTION

Former Member
0 Kudos
138

Hi,

Check the code below:

  • To remove quotes in the table

LOOP AT gt_dummy INTO gs_dummy. "#EC CI_LOOP_INTO_WA

gv_tabix = sy-tabix.

DO.

IF gs_dummy CA '"'.

REPLACE '"' WITH '' INTO gs_dummy.

ELSE.

WRITE gs_dummy TO gs_dummy NO-GAP.

CONDENSE gs_dummy.

MODIFY gt_dummy INDEX gv_tabix FROM gs_dummy.

EXIT.

ENDIF.

ENDDO.

ENDLOOP.

Regards

Kannaiah

13 REPLIES 13

Former Member
0 Kudos
138

Try:


REPLACE ALL OCCURRENCES OF ...

Rob

Former Member
0 Kudos
138

CLEAR flg_r.

flg_r = 'X'.

WHILE flg_r = 'X'.

SEARCH it_upfile2-csvdata FOR '"'.

IF sy-subrc = 0.

flg_r = 'X'.

REPLACE '"' WITH '' INTO it_upfile2-csvdata.

<b>exit.</b>

ELSE.

CLEAR flg_r.

ENDIF.

ENDWHILE.

flg_r = 'X'.

WHILE flg_r = 'X'.

SEARCH it_upfile2-csvdata FOR ' , '.

IF sy-subrc = 0.

flg_r = 'X'.

REPLACE ' , ' WITH ',' INTO it_upfile2-csvdata.

<b>exit.</b>

ELSE.

CLEAR flg_r.

ENDIF.

ENDWHILE.

0 Kudos
138

Thanks for the quick replies..

REPLACE ALL OCCURRENCES doesn't seem to pass the syntax checker in 46C, what's syntax on this command?

Seshu - I see that the exit will break the loop but I'm looking to replace all the " 's as there 10->30 of them in my field.

0 Kudos
138

Check the help. It will have all your answers.

Rob

former_member194669
Active Contributor
0 Kudos
138

Hi,

May this way.


      call function 'GET_COMPONENT_LIST'
        exporting
          program    = v_repid  "<< program Name
          fieldname  = v_fname  "<< it_upfile2
        tables
          components = i_comp.

  v_itabname = 'IT_UPFILE2-'.

  loop at it_upfile2.
  loop at i_comp.
     concatenate v_itabname i_comp-compname into v_field.
     condense v_field no-gaps.
     assign (v_field) to <lt_field>.
     replace : all occurrences of '"' in <lt_field> with ''.
  endloop.
     modify it_upfile2.  
  endloop.

aRs

Former Member
0 Kudos
138

See the simple example :

CLEAR d_text1.

DO.

SEARCH in_str FOR ';'.

IF sy-subrc <> 0.

IF NOT in_str IS INITIAL.

CONCATENATE d_text1 in_str INTO d_text1.

ENDIF.

EXIT.

ENDIF.

d_position = sy-fdpos + 6.

REPLACE ';' WITH '<SUB>;' INTO in_str.

CONCATENATE d_text1 in_str(d_position)

INTO d_text1.

d_strlen = strlen( in_str ).

d_strlen = d_strlen - d_position.

IF d_strlen LT 1. EXIT. ENDIF.

in_str = in_str+d_position(d_strlen).

CLEAR d_strlen.

ENDDO.

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos
138

How about the TRANSLATE keywork.

Translate str using '" '.

Regards,

Rich Heilman

0 Kudos
138

Here's what worked for me.. My second while/endwhile was only confusing things in removing the spaces that were left behind by the first while/endwhile, I replaced it with a condense fname no-gaps.

CLEAR flg_r.

flg_r = 'X'.

WHILE flg_r = 'X'.

SEARCH it_upfile2-csvdata FOR '"'.

IF sy-subrc = 0.

flg_r = 'X'.

REPLACE '"' WITH '' INTO it_upfile2-csvdata.

ELSE.

CLEAR flg_r.

ENDIF.

ENDWHILE.

CONDENSE it_upfile2-csvdata NO-GAPS.

0 Kudos
138

I suppose it will work, but why not try one of the simpler one line solutions? Future programmers will thank you.

Rob

0 Kudos
138

The while loop worked and I do split the record but later in the app as there is 70+ fields broken out which I wanted to avoid searching through invidually.

Rob - I would certainly like to use a simple one line solution however REPLACE ALL OCCURRENCES OF is absent in my ABAP help documentation perhaps this is not available in 46C.

0 Kudos
138

Rich's TRANSLATE should be in your version.

Rob

Former Member
0 Kudos
139

Hi,

Check the code below:

  • To remove quotes in the table

LOOP AT gt_dummy INTO gs_dummy. "#EC CI_LOOP_INTO_WA

gv_tabix = sy-tabix.

DO.

IF gs_dummy CA '"'.

REPLACE '"' WITH '' INTO gs_dummy.

ELSE.

WRITE gs_dummy TO gs_dummy NO-GAP.

CONDENSE gs_dummy.

MODIFY gt_dummy INDEX gv_tabix FROM gs_dummy.

EXIT.

ENDIF.

ENDDO.

ENDLOOP.

Regards

Kannaiah

Former Member
0 Kudos
138

First define your internal table with as many fields as you expect on this CSV file. Set up their lengths slightly more than their original lenght to accommodate the "s. After that, you first use the SPLIT command to split the record at ',' into your internal table fields.

LOOP AT it_upfile2.

SPLIT it_upfile2-csvdata AT ',' INTO itab-field1, itab-field2 ....

APPEND itab.

CLEAR itab.

ENDLOOP.

LOOP AT itab.

here you search and replace " in each field

ENDLOOP.