Can we fetch data from a global if name of the global is stored in a string?
I have created a global say ^myglobal and entered some data. I am storing the name of global in databases and fetching the name based on some criteria. I am getting "^myglobal" in return from databases but i am not able to fetch the data I stored in the global.
I tried set var = result.global which gives me "^myglobal" in var. Doing WRITE var will return "^myglobal" and not the data i stored in this global.
Comments
Read about indirection as well but wasn't able to get data using it. It was working for inserting
Indirection is your friend, which of course, works for globals, locals and other things too.
set ^myglobal="This is the root node"
set ^myglobal(12)="A node on level 1"
set ^myglobal(17)="More level 1 node"
set ^myglobal(17,1)="Data on the second level"
set ^myglobal(19,3)="More data on the second level"
; now start to play indirection
set someVariable="^myglobal" // the better way were: set someVariable=$name(^myglobal)
; and get the content of the above global
write @someVariable // --> This is the root node
write @someVariable@(12) // --> A node on level 1
write @someVariable@(17,1) // --> Data on the second level
; the same as above
set myVariable="^myglobal(17)" // Better: set myVariable=$name(^myglobal(17))
write @myVariable // --> More level 1 node
write @myVariable@(1) // Data on second level
; a bit overcomplicated
set string1="^"
set string2="myglobal"
write @(string1_string2_"("_(15+4_","_(1+2))_")") // --> More data on second level
So just (re)read the docu about indirection...
Thanks @Julius Kavay
In addition to what @Julius Kavay suggests I also recommend to use $Name function.
e.g.
set myVariable="^myglobal(17)"
// equal to
set myVariable=$Name(^myglobal(17))
It helps to deal with all the quotes/double quotes and also deal with variables that contain global adresses.
Absolutely right. It's more readable, without stumbling over millions of quotes and parenthesis during constructing (concatenate) the name of that globals ;-)
I used the "string version" to show him/her, the global can be used even if the name is given as a string.