字符串替换

时间:2016-03-28 18:52:08

标签: intersystems-cache mumps

在您看来,在不使用$ R的情况下更换字符串中的内容的最佳方法是什么?我写了一篇全球文章,我试图用PAUL代替PETER,但不是用$ R。这是我认为可行的迭代,但它只是取代了第一个PETER。对于同一条线上的多个Peters,你们会建议什么?

Start  
SET ary="^XA"
SET queryary=$QUERY(@ary@(""))
WRITE !,@queryary
FOR   {
SET queryary=$QUERY(@queryary) 
    QUIT:queryary=""  
    w !,$p(@queryary,"PETER",1)_"PAUL"_$p(@queryary,"PETER",2,$l(@queryary,"PETER"))  

}
  QUIT

这是我的第二次尝试,但我仍然需要多次运行才能执行所有更改。我的循环中是否缺少某些东西?

  Start  
  N ary
  S ary="^XA"
  S queryary=$Q(@ary@(""))
  S FROM="PETER"
  S TO="PAUL"
  W !,@queryary
  F   S queryary=$Q(@queryary) Q:queryary=""  w !,@queryary   d 
  . f  s $E(@queryary,$F(@queryary,FROM)-$L(FROM),$F(@queryary,FROM))=TO_" "     Q:ary'["PETER"  
  QUIT

5 个答案:

答案 0 :(得分:2)

不使用$ replace很棘手。我使用$ find和$ extract函数...我用“BACH”替换“MOZART”

mozartdocument
s ^XA(1)="ONCE UPON A TIME A COMPOSER NAMED MOZART WROTE"
s ^XA(2)="THE 'MOZART PIANO CONCERTO NUMBER ONE'. MOZART"
s ^XA(3)="MOZART 12 MOZART HANDEL MOZART MOZART 12"
s ^XA(4)="MAN MOZART MUMPS MANY MUNCHKINS MOZART"
s ^XA(5)="MOVE ALONG, NOTHING TO SEE HERE!"
s ^XA(6)="123 MOZART 456"
s ^XA(7)="HAPPILY EVER AFTER!"

for z = 1:1:7 {
    do {
        set x = $find(^XA(z),"MOZART")
        set $extract(^XA(z),x-6,x-1)="BACH"
    } while x > 0
    write !,^XA(z)
} write !

答案 1 :(得分:1)

这个怎么样?

    ClassMethod PeterPaul()
{
    s ^XNAME(1)="PETER PIPER PICKED A PEPPER"
    s ^XNAME(2)="PETER ENJOYS PIZZA'. PETER" 
    s ^XNAME(3)="PETER WAS BORN IN 1982" 
    s ^XNAME(4)="PETER LIKES PIZZA AND FRENCH FRIES'. PETER" 
    s ^XNAME(5)="THE PETER WROTE A BOOK CALLED PETER ADVENTURES." 
    s ^XNAME(6)="THE PETER HAD THREE KIDS.' PETER JR AND PETER III"
    s ^XNAME(7)="PETER MARRIED MARY."
    s i=$O(^XNAME(""))
    while i'="" {
        s ^XNAME(i)=..Replace(^XNAME(i),"PETER","PAUL")
        s i=$O(^XNAME(i))
        }
    q
}

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
}

答案 2 :(得分:1)

如果您正在使用Cache并想要一个实用程序,%GCHANGE是一个非常强大的程序,只是为了执行您所描述的操作。我一直把它当作一个实用程序而从来没有从程序中调用它,但我相信有一些标签可以让你调用并传入你的参数。

另一件事是你在一个循环中使用多个间接,这将减慢你的程序。我建议将所有这些组合成一个字符串,并使用E(X)ecute命令对整个字符串进行间接寻址。您可以看到下面提供的示例。

我包含了两种不同的替换字符串的方法。一个使用$ P和$ L类似于Evgeny Shvarov建议的,第二种方法使用$ F和$ E.

第二种方法在全球100000个节点上平均执行速度提高33%,每个节点更换4次。

我将包含我的数据。和我写的测试功能。我在传统的MUMPS代码中编写了这些代码,因此它可以跨平台工作。

更新:我刚检查了GTM文档。 %GCE是一个类似的实用程序,可在GTM中使用。 更新:我更改REPLACE功能以正确解释C4xuxo描述的LISA到ELISA问题。它仍然比使用$ P $ L表现更快。

更新:调整REPLACE函数中PS的值以修复错误;

;GLOBAL REPLACE METHOD 
GLBREPLACE(GLB,STR1,STR2) ;(GLOBAL NAME, STRING TO MATCH, STRING TO REPLACE WITH)
 S CMD="N I S I="""" F  S I=$O("_GLB_"(I)) Q:I=""""  S "_GLB_"(I)=$$REPLACE("_GLB_"(I),"""_STR1_""","""_STR2_""")"
 X CMD Q

;STRING REPLACE METHOD
REPLACE(STR,V1,V2) ;(INPUT STRING, STRING TO MATCH, STRING TO REPLACE WITH)
 N I,L,F1,F2,PS S PS=0,L=$L(STR,V1) F I=1:1:L-1 S F2=$F(STR,V1,PS),F1=F2-$L(V1),$E(STR,F1,F2-1)=V2,PS=F2+$L(V2) 
 Q STR



;======================================================================
;ADDITINAL FUNCTIONS

;THIS IS AN ALTERNATE METHOD, DOESN'T ADDRESS THE LISA TO ELISA PROBLEM
REPLACE2(STR,V1,V2) 
 N I F I=1:1:$L(STR,V1)-1 S STR=$P(STR,V1)_V2_$P(STR,V1,2,$L(STR,V1))
 Q STR

TESTGLBREPLACE ;THIS FUNCTION TESTS GLBREPLACE AND MEASURS PERFORMANCE
 S STIM=$ZTS S COUNT=100000
 D GENDATA(COUNT),GLBREPLACE("^XA","Peter","PAUL")
 S ETIM=$ZTS,TIMDIF=$P(ETIM,",",2)-$P(STIM,",",2),OCCURS=COUNT*4
 W !,"REPLACED "_OCCURS_" OCCURRENCES IN "_TIMDIF_" SECONDS"
 Q

GENDATA(L) ;THIS FUNCTION GENERATES DATA FOR A GIVE COUNT(L=INTEGER)
 F I=1:1:L S ^XA(I)="Peter Piper picked a peck of pickled peppers; A peck of pickled peppers Peter Piper picked; If Peter Piper picked a peck of pickled peppers, Where's the peck of pickled peppers Peter Piper picked"
 Q

答案 3 :(得分:0)

不幸的是我还不能发表评论,这应该更像是对先前解决方案的评论以及关于什么是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

答案 4 :(得分:0)

腮腺炎发育委员会会议记录中引用的标准REPLACE为$$ REPLACE ^ XLFSTR()。我经常使用它作为printf模拟器。


REPLACE(IN,SPEC) ;See $$REPLACE in MDC minutes.
         Q:'$D(IN) "" Q:$D(SPEC)'>9 IN N %1,%2,%3,%4,%5,%6,%7,%8
         S %1=$L(IN),%7=$J("",%1),%3="",%6=9999 F  S %3=$O(SPEC(%3)) Q:%3=""  S %6(%6)=%3,%6=%6-1
         F %6=0:0 S %6=$O(%6(%6)) Q:%6'>0  S %3=%6(%6) D:$D(SPEC(%3))#2 RE1
         S %8="" F %2=1:1:%1 D RE3
         Q %8
         ;
RE1      S %4=$L(%3),%5=0 F  S %5=$F(IN,%3,%5) Q:%5

以下是如何使用它的参考:

http://hardhats.org/kernel/html/x-replace%5Exlfstr.shtml