Question Sukhpreet Singh · 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

Comments

Sukhpreet Singh · Jul 15, 2023

Read about indirection as well but wasn't able to get data using it. It was working for inserting

0
Julius Kavay · Jul 15, 2023

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

0
Evgeny Shvarov  Jul 17, 2023 to 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.

0
Julius Kavay  Jul 17, 2023 to Evgeny Shvarov

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.

0