匹配模式间接寻址
匹配模式间接寻址是间接寻址的一种特殊形式。间接运算符替换模式匹配。间接的值必须是有效的模式。
- 间接寻址与模式匹配一起使用,以检查有效的15位身份证或18位身份证。
ClassMethod Indirection5(id) { s pattern = "18N" s:($length(id) = 15) pattern = "15N" if id ? @pattern { w !,"有效身份证" } } 复制代码
在模式匹配中使用间接寻址是定位应用程序中使用的模式匹配的一种便捷方式。在这种情况下,可以将模式存储在单独的变量中,然后在实际的模式匹配测试中间接引用它们。只需要修改模式匹配变量本身。
参数间接寻址
在参数间接寻址中,间接寻址计算一个或多个命令参数。
ClassMethod Indirection6() { s a = "var1",b = "var2 = 3 * 4 + var1" s @a = 5*6 s @b w "a = ",a,! w "b = ",b,! w @a,! s var2 = 3 s var1 = 6 w @b,! } 复制代码
下标间接寻址
下标间接寻址是名字间接寻址的扩展形式。在下标间接寻址中,间接寻址的值必须是本地或全局数组节点的名称。下标间接在语法上不同于其他形式的间接。下标间接使用以下格式的两个间接运算符:
@array@(subscript) 复制代码
ClassMethod Indirection7() { k ^color s ^color(1) = "red" s ^color(2) = "yellow" s ^color(3) = "black" s ^color(4) = "green" s ^color(5) = "write" s ^color(1, 1) = "red1" s ^color(1, 2) = "red2" s ^color(1, 3) = "red3" s ^color(1, 4) = "red4" s ^color(1, 5) = "red5" zw ^color s global = "^color" s node = "1" s sub = "1" w @global@(node, sub),! //w @global(node, sub),! s ID = "" for { s ID = $o(^color(ID)) q:(ID = "") s data = $zr w @data,! s sub = "" for { s sub = $o(^color(ID , sub)) q:(sub = "") w "Indirection:" _ @global@(ID, sub),! } } } 复制代码
- 自定义全局变量名称,节点名称使用下标间接寻址输出。
/// Sample.PersonD ClassMethod Indirection8() { r !,"global bame: ", name r !,"start: ", start r !,"end: ", end s array = "^" _ name s subNode = "Employee" for i = start : 1:end { if ($d(@array@(i))) { //w "@array@("_ i _"):" _ @array@(i),! w "@array@("_ i _"):" _ $li(@array@(i), 3),! } if ($d(@array@(i, subNode))){ //w "@array@("_ i _ "," _ subNode _ "):" _ @array@(i, subNode),! w "@array@("_ i _"):" _ $li(@array@(i, subNode), 1),! } } q }