replace command

stuartb42

New Member
Hello,

I am doing a replace on a field so I can strip out various CHR such as carriage returns which I do not need.

Using the following code at present:

STRING(REPLACE(REPLACE(ACCESS-INFO,CHR(13),"[CR]"),CHR(10),"[LF]"))

Does anybody know a better way of running multiple replaces on a string field. Think the way I have it may be a tad slow as some of my programs have this code in over 100 times. Not sure a double replace like this is the best way to acheive the same outcome.

Cheers

Stuart
 
stuartb42 said:
Hello,
Does anybody know a better way of running multiple replaces on a string field. Think the way I have it may be a tad slow as some of my programs have this code in over 100 times. Not sure a double replace like this is the best way to acheive the same outcome.

if you've got multiple replaces with more than two characters i think the following approach will be better

Code:
DEFINE VARIABLE len AS INTEGER    NO-UNDO.
DEFINE VARIABLE i AS INTEGER    NO-UNDO.
DEFINE VARIABLE access-info AS CHARACTER  NO-UNDO.
len = LENGTH(ACCESS-INFO).
 
DO i = 1 TO len:
   IF SUBSTR(ACCESS-INFO,i,1) = CHR(13) THEN
   DO:
      SUBSTR(ACCESS-INFO,i,1) = "[CR]".
      len = len + 3.
      i = i + 3.
      NEXT.
   END.
 
   IF SUBSTR(ACCESS-INFO,i,1) = CHR(10) THEN
   DO:
      SUBSTR(ACCESS-INFO,i,1) = "[LF]".
      len = len + 3.
      i = i + 3.
      NEXT.
   END.
END.
there's no other way to do it in 4gl now but if it's critical you should consider an external call.
 
string operations are extremely low cost (microsecond operations) imo in this case simplicity is a far more important factor.

the biggest overhead in writing low level operations in p4gl is that its an interpreted language. and other str functions probably have just as much overhead.

... it's not exactly assembly code
 
Back
Top