﻿ how can i achive that? | InterSystems Developer Community | Caché

# how can i achive that?

## Primary tabs

Caché

i want to achive a function like that:

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

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

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

thx :)

### Replies

`````` 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...

thx, then how can i retravel the nodes if i do not know  the levels? data like this:

tmp(1,2,3)=""

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

tmp(1,2,3,4,5)=""

tmp(1,2,3,4,5,...)=""

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

Write, for example, a method like this:

``````ClassMethod(ref)
{
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

i tried your method but it is a endless loop

I have no idea how you did your endless loop, but the above code can't do that.

figger it out     thx  :)

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.

set a="1.2.3.4"   x "set tmp("_\$tr(a,".",",")_")="""" "   zw tmp

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

if 1 2 3 4 5 … is integer

``````set in = "1.2.3"
set separator = "."
set separatorOut  = ","
set out = "tmp(" _ \$lts(\$lfs(in, separator), separatorOut) _ ")"``````

If you're sure that separator length is always equal you can use \$replace, and if you're sure that separator is one character long you can use \$translate.