Question
· Jul 17, 2017

Use a global stored in variable.

Greetings, 

How can I store a global name in a variable, ie: x="^G" and then use x like this, as if it were the global:  y=$o(x)  ?

Please advise,

Thanks... 

Discussion (3)0
Log in or sign up to continue

Indirection is one of the really cool features of Cache Object Script.

Try this:

1. Simple indirection

>s global="^someglobal"

>w @global

This is the same as 'w ^someglobal"

2. Name indirection with $Q

>s global="^someglobal"

>f  s global=$Q(@global) q:global=""  w !,global,"=",@global

This will dump all of the data in ^someglobal, regardless of the number of subscripts.

3. Subscript indirection

>s global="^someglobal"

>s sub="

>f  s sub=$o(@global@(sub)) q:sub=""  w !,sub

This dumps the 1st level subscript values.

---

Search for "Indirection" in your Cache Documentation.  There is lots more...

Have fun,

DaveM

Hi, Russel!

You can always use $name for any operations with  global name, either simple as ^G or with subscripts as "^G(1,"something").

It is intended for this.

In your case it would look like:

set x=$Name(^G)

or you can set global name with subscript, e.g.:

Set x=$Name(^G(1,"second"))

It's very convenient to use indirection operator @ in this case to deal with variable with global name inside.

E.g. you can refer to any subscript with global inside x, like this:

set @x@(1)=2

which would be equal to:

set ^G(1,"second",1)=2

And with your $Order example you can have:

USER> set x=$Na(^G(1,"second"))
USER> set @x@(1)=2
USER> set y=$O(@x@(""))
USER> write y
1 

HTH