不幸的是,我还不能发表评论,这应该更像是对先前解决方案的评论以及关于 Cache 生成的实际腮腺炎的问题。因此,如果有人在下面回复并确认我的可疑之处会很好,因为我认为以前的解决方案存在错误。
所以假设 Cache 编译了下面的解决方案:
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
}
对于这样的事情:
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
from
这段代码中有一个严重的错误,当我的实际变量包含在中时会导致无限循环to
,
例如,将“LISA”更改为“ELISA”、“ELISABETH”、“ALISA”、“MELISA”。
下面使用的示例将 DAN 更改为 DANIEL。
在 GTM 上测试(循环在 10 次迭代后手动中断,否则将是无限的):
GTM>W $$REPLACE^ZZTEST("DAN SMITH","DAN","DANIEL")
DANIELIELIELIELIELIELIELIELIELIEL SMITH
考虑到这一点,我提出如下建议:
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
在 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
当然这不会是最终解决方案,因为它仍然包含错误,例如生成名称 LISABETH....
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
为了绕过这个问题,需要添加额外的逻辑来理解如果名称在开头需要是“NAME”如果是在结尾“NAME”,否则在中间“NAME”。
类似的东西(可能可以优化):
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
在 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
但是,如果您决定或收到以下输入,可能仍无法满足您的所有需求:
GTM>W $$REPLACE2^ZZTEST("ELISA,SMITH","ELISA","LISA")
ELISA,SMITH