Question
· Jul 15, 2023

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.

Product version: IRIS 2023.1
Discussion (5)2
Log in or sign up to continue

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