SEARCH(where,what)
; Search string in a Global or Array
I $G(where)="" Q
I $G(what)="" Q
F S where=$Q(@where) Q:where="" D
. I ((where[what)!(@where[what)) D
.. W where,"=",@where,!
.. W $E(where,$F(where,"("),$L(where)-1),!
Q
要提取下标(键或键,因为可以存在多个)我会使用 $E 和 $F 因为当“(”或“)”是下标的一部分时会出现问题。
W $E(where,$F(where,"("),$L(where)-1)
在 GTM 上测试。
正常输入:
GTM>ZWR ^ZNAME
^ZNAME("first,last")="Second Street"
^ZNAME("name,surname")="First Street"
GTM>D SEARCH^ZZTEST("^ZNAME","last")
^ZNAME("first,last")=Second Street
"first,last"
GTM>D SEARCH^ZZTEST("^ZNAME","Street")
^ZNAME("first,last")=Second Street
"first,last"
^ZNAME("name,surname")=First Street
"name,surname"
GTM>D SEARCH^ZZTEST("^ZNAME(""first,last"")","Street")
^ZNAME("name,surname")=First Street
"name,surname"
GTM>D SEARCH^ZZTEST("^ZNAME",",sur")
^ZNAME("name,surname")=First Street
"name,surname"
作为奖励,您还可以使用它来搜索本地数组:
GTM>ZWR ARRAY
ARRAY(1)="Apple"
ARRAY(1,1)="Apple pie"
ARRAY(2)="Orange"
GTM>D SEARCH^ZZTEST("ARRAY","Apple")
ARRAY(1)=Apple
1
ARRAY(1,1)=Apple pie
1,1
GTM>D SEARCH^ZZTEST("ARRAY","pie")
ARRAY(1,1)=Apple pie
1,1
无效输入也没有问题:
GTM>K ARRAY
GTM>D SEARCH^ZZTEST("ARRAY","Apple")
GTM>D SEARCH^ZZTEST("ARRAY","")
GTM>D SEARCH^ZZTEST("ARRAY",)
GTM>D SEARCH^ZZTEST("ARRAY")
GTM>D SEARCH^ZZTEST()
GTM>D SEARCH^ZZTEST("^ZDOESNOTEXIST")
处理下标中的“(”和“)”:
GTM>ZWR ARRAY
ARRAY("()")=1
ARRAY("1(")=1
ARRAY("1()")="Orange"
ARRAY("1)")="Apple"
GTM>D SEARCH^ZZTEST("ARRAY","()")
ARRAY("()")=1
"()"
ARRAY("1()")=Orange
"1()"
GTM>D SEARCH^ZZTEST("ARRAY","Apple")
ARRAY("1)")=Apple
"1)"