Unfortunately I cannot yet post comments, and this was supposed to be more like a comment to previous solution and a question about what is the actual mumps generated by Cache. So if someone reply and confirm my suspicious below would be great, since I think that there is a bug on previous solution.
So assuming the Cache compiles the solution below:
ClassMethod Replace(str, from, to As %String)
{
while $F(str,from) {
s str=$P(str,from)_to_$P(str,from,2,$L(str,from))
}
quit str
}
To something like this:
REPLACE(str,from,to)
;
F I=1:1 Q:'$F(str,from) D
. S str=$P(str,from)_to_$P(str,from,2,$L(str,from))
Q str
There is a serious bug in this code that will result in a infinite loop when my actual from
variable is contained in to
,
Change for example "LISA" to "ELISA", "ELISABETH", "ALISA", "MELISA".
Example used below change DAN to DANIEL.
Tested on GTM (Loop manually interrupted after 10 iteration otherwise would be infinite):
GTM>W $$REPLACE^ZZTEST("DAN SMITH","DAN","DANIEL")
DANIELIELIELIELIELIELIELIELIELIEL SMITH
With this in mind I propose something like:
REPLACE2(str,from,to)
;
N str2
S str2=""
F I=1:1:$L(str,from)-1 D
. S str2=str2_$P(str,from)_to
. S str=$P(str,from,2,$L(str,from))
;add the last piece if it exists or in case nothing to replace add all.
Q str2_str
Tested in GTM:
GTM>W $$REPLACE2^ZZTEST("DAN SMITH","DAN","DANIEL")
DANIEL SMITH
GTM>W $$REPLACE2^ZZTEST("DAN SMITH DAN","DAN","DANIEL")
DANIEL SMITH DANIEL
GTM>W $$REPLACE2^ZZTEST("DAN SMITH DAN DAN DAN","DAN","DANIEL")
DANIEL SMITH DANIEL DANIEL DANIEL
GTM>W $$REPLACE2^ZZTEST("DAN SMITH DAN DAN DAN","DANA","DANIEL")
DAN SMITH DAN DAN DAN
Of course that will not be a final solution since it still contains bugs for example the name LISABETH is generated....
GTM>W $$REPLACE2^ZZTEST("ELISABETH SMITH","ELISA","LISA")
LISABETH SMITH
GTM>W $$REPLACE2^ZZTEST("ELISA ELISABETH SMITH ELISA","ELISA","LISA")
LISA LISABETH SMITH LISA
GTM>W $$REPLACE2^ZZTEST("ELISA ELISABETH SMITH ELISA"," ELISA","LISA")
ELISALISABETH SMITHLISA
GTM>W $$REPLACE2^ZZTEST("ELISA ELISABETH SMITH ELISA"," ELISA ","LISA")
ELISA ELISABETH SMITH ELISA
GTM>W $$REPLACE2^ZZTEST("ELISA ELISABETH SMITH ELISA"," ELISA","LISA")
ELISALISABETH SMITHLISA
GTM>W $$REPLACE2^ZZTEST("ELISA ELISABETH SMITH ELISA","ELISA ","LISA")
LISAELISABETH SMITH ELISA
To bypass this problem additional logic needs to be added to understand that if the name is in the beginning needs to be "NAME " if is at end " NAME", otherwise in the middle " NAME ".
something like (that can probably be optimized):
REPLACE2(str,from,to)
;
N from2,str2
S str2=""
S from2=" "_from_" "
; check if string begins with name
I $E(str,1,$L(from))_" "=(from_" ") S str2=to,str=$E(str,$L(from)+1,$L(str))
; search for name with spaces
F I=1:1:$L(str,from2)-1 D
. S str2=str2_$P(str,from2)_" "_to
. S str=" "_$P(str,from2,2,$L(str,from2))
; check if finishes with name
I $L(str)>=$L(from) D
. I $E(str,$L(str)-$L(from),$L(str))=(" "_from) S str2=str2_$E(str,1,$L(str)-$L(from))_to,str=""
.
Q str2_str ;add the last piece if it exists
Test on GTM:
GTM>W $$REPLACE2^ZZTEST("MELISA ELISA ELISABETH ALISA ELISA","ELISA","LISA")
MELISA LISA ELISABETH ALISA LISA
GTM>W $$REPLACE2^ZZTEST("MELISA ELISA ELISABETH ALISA ELISA","LISA","ELISA")
MELISA ELISA ELISABETH ALISA ELISA
GTM>W $$REPLACE2^ZZTEST("LISA MELISA ELISA ELISABETH LISA ALISA LISA","LISA","ELISA)
ELISA MELISA ELISA ELISABETH ELISA ALISA ELISA
GTM>W $$REPLACE2^ZZTEST("LISA MELISA ELISA ELISABETH LISA ALISA LISA","LISA","ELISA)
ELISA MELISA ELISA ELISABETH ELISA ALISA ELISA
But still may not attend to all of your needs if you decide or receive input like:
GTM>W $$REPLACE2^ZZTEST("ELISA,SMITH","ELISA","LISA")
ELISA,SMITH