Why use substitute

Dear All,

I want to know actual usage of substitute function. I have seen one old thread ("Remove leading 0 and removing dash") where substitute function is being used to add two strings and i think it could easily be replaced by string concatenation "+".

Along with that i ran both codes in multiple iteration and found SUBSTITUTE as little slower then adding two strings with "+" operator. below is code for the same:

Code:
DEFINE VARIABLE itime AS INT NO-UNDO EXTENT 5 INITIAL {&sequence}.
DEFINE VAR i AS INTEGER NO-UNDO.
DEFINE VAR txt AS CHAR INIT "03-001-11-01 PLT3" NO-UNDO.
DEFINE VAR txt1 AS CHAR INIT "03-001-11-01 PLT3" NO-UNDO.
DEFINE VARIABLE idx AS INTEGER   NO-UNDO.
DEFINE VARIABLE idx1 AS INTEGER   NO-UNDO.

itime[{&sequence}] = ETIME.     

DO i = 1 TO 1000000:

END.

itime[{&sequence}] = ETIME.     

DO i = 1 TO 1000000:


    ASSIGN 
        txt = LEFT-TRIM(txt, '0 ')
        idx = INDEX(txt, '-')
        txt = SUBSTITUTE('&1&2', SUBSTRING(txt, 1, idx), REPLACE(SUBSTRING(txt, idx, -1), '-', '')) NO-ERROR.
END.

itime[{&sequence}] = ETIME.     


DO i = 1 TO 1000000:

    ASSIGN 
        txt1 = LEFT-TRIM(txt, '0 ')
        idx1 = INDEX(txt, '-')
        txt1 = SUBSTRING(txt, 1, idx) + REPLACE(SUBSTRING(txt, idx, -1), '-', '') NO-ERROR.

END.

itime[{&sequence}] = ETIME.  

MESSAGE      
       itime[2] - itime[1] SKIP
       itime[3] - itime[2] SKIP       
       itime[4] - itime[3] SKIP              
    VIEW-AS ALERT-BOX.

Output for me is:

221
2880
2404

Please suggest.

Regards,
 

TomBascom

Curmudgeon
I use SUBSTITUTE() even for simple concatenation because of its handling of the unknown value. In almost all cases I prefer to have unknown turned into a literal “?” than to have the entire string become unknown.

The performance difference, while real, is generally not a significant portion of the user perception of performance. If I need things to go faster there are almost always going to be more profitable improvements to chase after.
 

tamhas

ProgressTalk.com Sponsor
This is a plus for other uses of substitute as well, e.g., some kind of slice and dice where one could assemble pieces, but it would require additional logic to detect and handle unknown values.
 

Bounty

New Member
Readability:
Code:
t1 = "File " + MyFile + " was deleted on " + STRING(TODAY) + " at " + STRING(TIME, "HH:MM").
 
t2 = SUBSTITUTE("File &1 was deleted on &2 at &3", MyFile, STRING(TODAY), STRING(TIME, "HH:MM")).

I definitely prefer SUBSTITUTE in these cases and if you need to translate texts it will make life easier too.
 
Top