Question
· Sep 21, 2023

Setting a global variable to change global reference

Currently I have a program that is going to be used to compare global nodes between namespaces. Using nested FOR statements I am stepping through global nodes and comparing between the namespaces, if the global values are different I then parse the node comparing each delimited segment.

This is working.

I want to now be able to set what global I am in as a variable to expand on this concept. 

 

Program architecture

1.Initializing program that has user prompts to change starting nodes. This program contains the FOR nests to step through nodes. If the Global value is different then we move to program #2.

2. Parsing program taking variables from the first program to compare that then returns to program 1. This program displays back the location variations in the delimited string.

 

Example Data structure:

^[NSL]Global1(a)="Test"

^[NSL]Global1(a,b)="66738,71469"

^[NSL]Global1(a,b,c)="Apple\Orange-Peach#ball\1#0#1#1"

 

^[NSR]Global1(a)="Live"

^[NSR]Global1(a,b)="66735,71369"

^[NSR]Global1(a,b,c)="ball\Peach-Orange#Apple\1#4#5#1"

 

 

I want to be able to store "Global1" as a variable so I can change what global I am using in my program.

Hypothetical concept that does not work:

S GlobalVar = "Global1"

S GlobalDataLeft = $G(^[NSL]GlobalVar(a,b,c))

S GlobalDataRight= $G(^[NSR]GlobalVar(a,b,c))

 

 

Is there something that I can do that accomplishes this idea? I was hoping of a nice and easy way to update my code :)

Still feeling green so I am sorry if my terminology is not 100%!

Product version: IRIS 2021.1
$ZV: 2021.1.2
Discussion (3)1
Log in or sign up to continue

Hi @Ryan Pflughaupt

You need to use the $NAME function set the global variable into local variable to process global names extended global reference dynamically. There is no problem with setting global name in local variable if it's belongs from the current namespace. Use indirection( @ )  operator to do $query,$order,$data string functions and get values from it(Suggest to use $NAME for current namespace global as well) . 

USER>set GlobalVar=$NAME(^["IRISMYDEV"]TEST)
 
USER>zwrite @GlobalVar
^["IRISMYDEV"]TEST("a")="Test"
^["IRISMYDEV"]TEST("a","b")="66739,34774"
^["IRISMYDEV"]TEST("a","b","c")="Apple\Orange-Peach#ball\1#0#1#1"

USER>set sub=$query(@GlobalVar) for  quit:sub=""  write sub,! set sub=$Query(@sub)
^["IRISMYDEV"]TEST("a")
^["IRISMYDEV"]TEST("a","b")
^["IRISMYDEV"]TEST("a","b","c")
 

by using this you can take the values. Check last global reference by using the $zreference function.

Thanks for the reply!

Looking at your example I am wanting to have "TEST" be a variable that can be set. Right now my nested FOR and $Order loops are starting at a static point. I want to be able to change my starting global without just writing another program.

USER>set GV = "TEST"
USER>set GlobalVar=$NAME(^["IRISMYDEV"]GV)
 
USER>zwrite @GlobalVar
^["IRISMYDEV"]TEST("a")="Test"
^["IRISMYDEV"]TEST("a","b")="66739,34774"
^["IRISMYDEV"]TEST("a","b","c")="Apple\Orange-Peach#ball\1#0#1#1"

USER>set sub=$query(@GlobalVar) for  quit:sub=""  write sub,! set sub=$Query(@sub)
^["IRISMYDEV"]TEST("a")
^["IRISMYDEV"]TEST("a","b")
^["IRISMYDEV"]TEST("a","b","c")

You need to pass the subscripts also part of the $NAME itself to make it dynamic instead start from the beginning

USER>s subscript="a" ;your dynamic subscript 
 
USER>set GlobalVar=$NAME(^["IRISMYDEV"]TEST(subscript)) ; pass the subscript into $NAME
 
USER>write GlobalVar
^["IRISMYDEV"]TEST("a")
USER>set sub="" write $o(@GlobalVar@(sub),1,data) ; next subscript of "a"
b
USER>write @GlobalVar
Test
USER>write GlobalVar
^["IRISMYDEV"]TEST("a")