Chapter 3 Using Multidimensional Storage (Global Variables) (2)

Iterate over the data in a global variable

There are many ways to iterate over data stored in global variables.

$ORDER(next/previous) function

The ObjectScript $Order function allows sequential access to each node in the global.

The $ORDER function returns the value of the next subscript of the given level (subscript number). For example, suppose the following global Settings are defined:

 Set ^Data(1) = ""
 Set ^Data(1.1) = ""
 Set ^Data(1.2) = ""
 Set ^Data(2) = ""
 Set ^Data(2.1) = ""
 Set ^Data(2.2) = ""
 Set ^Data(5.1.2) = ""
Copy the code

To find the first first-order subscript, we can use:

 SET key = $ORDER(^Data(""))
Copy the code

This returns the first first-level index after the empty string (” “). (An empty string is used to represent the subscript value before the first entry; As a return value, it is used to indicate that there is no subsequent subscript value. . In this case, key will now contain the value 1.

We can find the next first-level subscript by using the 1 or key in the $ORDER expression:

 SET key = $ORDER(^Data(key))
Copy the code

If the initial value of key is 1, this statement sets it to 2(because ^Data(2) is the next first-level subscript). Executing this statement again sets the key to 5 because this is the next first-level subscript. Note that 5 is returned even if there is no Data directly stored in ^Data(5). Executing this statement again sets key to an empty string (” “), indicating that there are no more first-level subscripts.

By using the additional subscript with the $ORDER function, you can iterate over different subscript levels. $order returns the next value of the last index in its argument list. Using the above data, the statement reads as follows:

 SET key = $ORDER(^Data(1.""))
Copy the code

Set the keyword to 1, because ^Data(1, 1) is the next second-level subscript. Executing this statement again sets the KEY to 2, because this is the next second-level subscript. Executing this statement again sets the key to “”, indicating that there are no more secondary subscripts under the node ^Data(1).

Use the $ORDER loop

The following ObjectScript code defines a simple global variable and loops through all of its first-level subscripts:

/// w ##class(PHA.TEST.Global).ReadGlobalSimpleFor()
ClassMethod ReadGlobalSimpleFor(a)
{
	// Clear ^Data in case it has Data
	Kill ^Data

	// Fill in ^Data with sample Data
	For i = 1:1:100 {
		// Set each node to a random name
		Set ^Data(i) = ##class(%PopulateUtils).Name} // Loop over each node to find the first nodeSet key = $Order(^Data(""))

	While (key '= "") { Write "#", key, " ", ^Data(key),! Set key = $Order(^Data(key))} q ""}Copy the code
DHC-APP>w ##class(PHA.TEST.Global).ReadGlobalSimpleFor() # 1Edwards.Barbara T. # 2Ragon.Kevin K# 3.Avery.Josephine U# 4.Townsend.Buzz R. # 5Joyce.Quentin V# 6.Xenia.Ted F. # 7Chadwick.Wilma N. # 8Duquesnoy.Orson A# 9.Uberoth.Orson X# 10.Jones.Joe O# 11.Hills.Barb R# 12.Yakulis.Pat J# 13.Tesla.Al P# 14.Goncharuk.Sam J# 15.Presley.Amanda D# 16.Olsen.Kristen I# 17.Roentgen.John T# 18.Minichillo.Elmo N# 19.Koivu.Patrick R# 20.Harrison.Lawrence I# 21.Page.Agnes P# 22.Wijnschenk.Hannah L# 23.Chesire.Bart S# 24.Klingman.Liza K# 25.Smyth.Imelda J# 26.Alton.Filomena L# 27.Minichillo.Charles U# 28.Nichols.Jeff W# 29.O'Rielly.Thelma X# 30.Schaefer.Kristen G# 31.Black.Filomena R# 32.Vivaldi.Xavier B# 33.Allen.Phyllis U# 34.Mastrolito.Zelda Z# 35.Quilty.Jane V# 36.Zevon.Maureen H# 37.O'Rielly.Maureen C# 38.Olsen.Robert W# 39.Page.Milhouse D# 40.Nelson.Dick R# 41.Ironhorse.Danielle I# 42.Tweed.Rhonda T43 #.Quincy.Terry L# 44.Tsatsulin.Jocelyn C# 45.Yeats.Michelle E# 46.Jackson.Paul V# 47.Humby.Dave I# 48.Kelvin.Natasha R# 49.Kelvin.Kyra R# 50.Yoders.Agnes R# 51.Tesla.Amanda F# 52.Harrison.Christen T# 53.Allen.Nataliya J# 54.Xenia.Diane W# 55.Xenia.Phyllis E# 56.Isaksen.Pam D# 57.Waterman.Charles M# 58.Peters.Sophia N# 59.Peterson.Bart B# 60.Eastman.Edward S. # 61Young.Belinda F. # 62White.Fred G. # 63Ubertini.Lola U. # 64Uhles.Xavier T. # 65Quine.Phyllis T. # 66Hernandez.Umberto B. # 67Allen.Zelda S. # 68Harrison.David Z. # 69Harrison.Danielle T. # 70Ott.Dick D. # 71Lennon.Joe Y. # 72Quigley.Alfred M. # 73Klausner.Mario J. # 74Tsatsulin.Emily S. # 75Anderson.Edward R. # 76Lennon.Fred H. # 77DeSantis.Molly J. # 78Browne.Dave H. # 79Cunningham.Buzz L. # 80Ingersol.Edgar G. # 81Paraskiv.Linda O. # 82Beatty.Kim H. # 83Quilty.Wilma P. # 84Dunlap.Jules I. # 85Waterman.Buzz D. # 86Edison.Kim C. # 87Eagleman.Michael N. # 88Huff.Hannah K. # 89Vanzetti.Maria E. # 90Zampitello.Angela Q. # 91Anderson.Angela Z. # 92Isaacs.Charlotte Q. # 93O'Donnell.Paul A. # 94Underman.Zeke R. # 95Schultz.James I. # 96Chadbourne.Janice N. # 97Lennon.William T. # 98Vonnegut.Pam V. # 99Miller.Patricia T. # 100Hills.Charles C.
Copy the code

Additional $ORDER parameters

The ObjectScript $ORDER function takes optional second and third arguments. The second argument is a directional flag indicating in which direction you want to traverse the global variable. The default value 1 specifies forward traversal, while -1 specifies reverse traversal.

The third argument (if present) contains a local variable name. If the node found by $ORDER contains data, the data found is written to this local variable. This is more efficient when you are in a global loop and you are interested in node values and subscript values.

$QUERY function

If you need to access each node and child node in a global variable, moving up and down on the child nodes, use the ObjectScript $Query function. (Alternatively, you can use a nested $ORDER loop).

$QueryThe function takes a global variable reference and returns a string containing the global reference to the next node in the global variable (or if there are no subsequent nodes)""). If you want to use$QUERYThe value returned must use the ObjectScript indirect operator(@).

For example, suppose the following global Settings are defined:

 Set ^Data(1) = ""
 Set ^Data(1.1) = ""
 Set ^Data(1.2) = ""
 Set ^Data(2) = ""
 Set ^Data(2.1) = ""
 Set ^Data(2.2) = ""
 Set ^Data(5.1.2) = ""
Copy the code

Here is the call to $QUERY:

 SET node = $QUERY(^Data(""))
Copy the code

Set the node to the string “^Data(1)”, which is the address of the first node in the global. Then, to get the next node in the global, call $QUERY again and use the indirect operator on the node:

At this point, the node contains the string ^Data(1,1).

The following example defines a set of global variable nodes and then iterates through them using $QUERY while writing the address of each node:

/// w ##class(PHA.TEST.Global).ReadGlobalSimpleQuery()
ClassMethod ReadGlobalSimpleQuery(a)
{
	Kill ^Data // Make sure ^Data is null

	// Put some Data into ^Data //
	Set ^Data(1) = ""
	Set ^Data(1.1) = ""
	Set ^Data(1.2) = ""
	Set ^Data(2) = ""
	Set ^Data(2.1) = ""
	Set ^Data(2.2) = ""
	Set ^Data(5.1.2) = ""

	// Now browse ^Data to find the first node
	Set node = $Query(^Data(""))
	While (node '= "") { Write node,! Set node = $Query(@node)} q ""}Copy the code
DHC-APP>w ##class(PHA.TEST.Global).ReadGlobalSimpleQuery(^)Data^ (1)Data^ (1, 1)Data^ (1, 2)Data(2)
^Data^ (2, 1)Data^ (2, 2)Data(5,1,2)
Copy the code