Variables in Global Name

#Globals #Other

I have a number of globals I want to loop through depending on a passed in string...

 

^A("SYSTEM")

^B("SYSTEM")

^C("SYSTEM")

^D("SYSTEM")

^E("SYSTEM")

 

I am passing in a string of "A:C:D"

Piecing that to pull out A,C,D, then want to use that to pull out A,C,D from above, but having problems setting the name of the global to be the name of the variable I have pulled out of the string

How do I set this?

I have tried ^varname("SYSTEM")

also tried setting a variable to have the value "^varname" and using that

 

Thanks

  • 00
  • 0
  • 165
  • 3

Replies

Hi

So you can use global indirection here:

you can set a variable to the name of a global

set gbl="^MyGlobalName"

you can then do either of the following:

if $(@gbl)#10 {write !,"Global: ",gbl," has a value of ",@gbl}
else {write !,"The global: ",gbl," is not defined"}

or you can use it to reference nodes within the global root:

for gbl="^A","^B","^C","^D","^E" {write !,"Global: ",gbl," for the node: ",gbl,"(""SYSTEM"") has a value: ",@gbl@("SYSTEM")

you can also do this:

set gbl="^NigelGlobal(""Subscript1"""_","_"""Subscript2"")"
set y=$o(@gbl@(y)

which effectively reads

set y=$o(^NigelGlobal("Subscript1","Subscript2",y) )

so before this I executed the following line of code:

set ^NigelGlobal("Subscript1","Subscript2","This is a Test")=""
set y=""
set y=$o(@gbl@(y))
if y'=""  write !,y
This is a Test

if you want to use this across namespaces then you need to do the following:

set ns="My other Namespace Name" e.g. set ns="DEV", my current Namespace is "QC"
set gbl1="^MyGlobalA",gbl2="^["""_ns_"""]MyGlobalB"
merge @gbl1@("Node A")=@gbl2("Node B")

this then reads as:

merge ^MyGlobalA("NodeA")=^["DEV"]MyGlobalB("NodeB")

Yours

Nigel

Just as a note, where you are manually building up your global in gbl you could use the $name function instead, for example...

set gbl="^NigelGlobal(""Subscript1"""_","_"""Subscript2"")"

can be

set gbl=$name(^NigelGlobal("Subscript1","Subscript2"))

which makes it easier to handle quotes etc...

update: ignore the very odd formatting this editor applied to the objectscript.