· Mar 14, 2020

how can i achive that?

i want to achive a function like that:

"1.2.3"   ->   tmp(1,2,3)=""

""   ->   tmp(1,2,3,4)=""

""   ->   tmp(1,2,3,4,...)=""

thx :)

Discussion (10)1
Log in or sign up to continue
 set x="1.2.3", y=$name(tmp)
 for i=1:1:$length(x,".") { set y=$name(@y@($piece(x,".",i))) }
 set @y=""
 kill x,y,i

You can put al the above in one line, if you are in terminal. Or put it in a method

ClassMethod setTemp(x) [ PublicList = tmp]
 set y=$name(tmp)
 for i=1:1:$length(x,".") { set y=$name(@y@($piece(x,".",i))) }
 set @y=""

Or you take the shorthand

set x="1.2.3", @("tmp("_$tr(x,".",",")_")=""""")

But this works only as long as x contains number_period_numeber_period_...No leading zeros, no alphabetics...

the functions $order() and $query() are your best friends ;-))

Write, for example, a method like this:

  if $data(@ref)#10 write ref,"=",@ref,! // do something with this node
  set i=""
  for  set i=$order(@ref@(i)) quit:i=""  do ..Show($name(@ref@(i)))

So, for testdata like

set tmp(1,2,3)="Joe"

set tmp(1,2,3,4,1)="Paul"

set tmp(1,2,3,4,2)="Paula"

set tmp(1,2,3,5,6)="Tom"

start some testing

do ##class(yourclass).Show($na(tmp))  // shows all entries

do ##class(yourclass).Show($na(tmp(1,2)))  // also shows all entries

do ##class(yourclass).Show($na(tmp(1,2,3)))  // shows Joe

do ##class(yourclass).Show($na(tmp(1,2,3,4))  // shows Paul and Paula

do ##class(yourclass).Show($na(1,2,3,5)))  // shows Tom

do ##class(yourclass).Show($na(tmp(1,2,3,5,6)))  // shows Tom

do ##class(yourclass).Show($na(tmp(2,3)))  // shows nothing

Now, I hope, you can figure out how to get out your desired data

Use $QUERY, $QL and $QS to traverse the array

ClassMethod Array()
; Note: $query only works on globals or
; public variables
; not local array in an object

set ^tmp(1,2,3)="Joe"
set ^tmp(1,2,3,4,1)="Paul"
set ^tmp(1,2,3,4,2)="Paula"
set ^tmp(1,2,3,5,6)="Tom" ; use indirection to traverse the global

; get the data nodes
set sub="^tmp"
set sub=$query(@sub)
while sub]""
      write !,sub,"=",@sub

      ; use $QL to get the number of subscripts for the data node
      set subCnt=$ql(sub)
      w !,"Number of subscripts = ",subCnt

      ; use $QS to get the subscript value
      for i=1:1:subCnt !,"subscript ",i,"=",$qs(sub,i)

      ; get the next data node
     set sub=$query(@sub)

Hope this helps.