12.07.2015 Views

Using Caché Globals - InterSystems Documentation

Using Caché Globals - InterSystems Documentation

Using Caché Globals - InterSystems Documentation

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Using</strong> Multidimensional Storage (<strong>Globals</strong>)// clear ^Data in case it has dataKill ^Data// fill in ^Data with sample dataFor i = 1:1:100 {// Set each node to a random person's nameSet ^Data(i) = ##class(%PopulateUtils).Name()}// loop over every node// Find first nodeSet key = $Order(^Data(""))While (key '= "") {// Write out contentsWrite "#", key, " ", ^Data(key),!}// Find next nodeSet key = $Order(^Data(key))3.5.1.2 Additional $ORDER ArgumentsThe <strong>Caché</strong> ObjectScript $ORDER function takes optional second and third arguments. The secondargument is a direction flag indicating in which direction you wish to traverse a global. The default,1, specifies forward traversal, while –1 specifies backward traversal.The third argument, if present, contains a local variable name. If the node found by $ORDER containsdata, the data found is written into this local variable. When you are looping over a global and you areinterested in node values as well as subscript values, this operates more efficiently.3.5.2 Looping Over a GlobalIf you know that a given global is organized using contiguous numeric subscripts, you can use a simpleFor loop to iterate over its values. For example, in <strong>Caché</strong> Basic:For i = 1 To 100Print ^Data(i)Nextor the equivalent in <strong>Caché</strong> ObjectScript:For i = 1:1:100 {Write ^Data(i),!}Generally, it is better to use the $ORDER function described above: it is more efficient and you donot have to worry about gaps in the data (such as a deleted node).3.5.3 The $QUERY FunctionIf you need to visit every node and subnode within a global, moving up and down over subnodes, usethe <strong>Caché</strong> ObjectScript $QUERY function. (Alternatively you can use nested $ORDER loops).22 <strong>Using</strong> <strong>Caché</strong> <strong>Globals</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!