11.07.2015 Views

Caché ObjectScript Reference - InterSystems Documentation

Caché ObjectScript Reference - InterSystems Documentation

Caché ObjectScript Reference - 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.

ExampleThis example presents a generalized routine for outputting the data values for all the nodesin a user-specified array. It can accommodate arrays with any number of levels. The codeperforms the same operation as the code shown in the example under the $ORDER function.Instead of requiring 23 lines, however, it requires only six and is not restricted as to thenumber of levels it can handle.Start READ !,"Array name: ",ary QUIT:ary=""SET queryary=$QUERY(@ary@(""))WRITE !,@queryaryFOR {SET queryary=$QUERY(@queryary)QUIT:queryary=""WRITE !,@queryary}WRITE !,"Finished."QUITThe first SET command uses $QUERY with subscript indirection to initialize the globalreference to the first existing node that contains data. For more information, refer to Indirectionin Using <strong>Caché</strong> <strong>ObjectScript</strong>. (Like $ORDER, $QUERY accepts a null string to designatethe first subscript in an array.) The first WRITE command outputs the value of the first nodefound. If it were omitted, the first node would be skipped.In the FOR loop, $QUERY is used to retrieve the next node and update the global reference,whose contents are then output by the WRITE command. The postconditional QUIT terminatesthe loop when it finds a null string (""), indicating that $QUERY has reached the endof the array.No $DATA tests are required, unless you wish to distinguish between pointer nodes($DATA=11) and terminal nodes ($DATA=1).NotesUsing $QUERY to Traverse an ArrayUsed repetitively, $QUERY can traverse an entire array in left-to-right, top-to-bottom fashion,returning the entire sequence of defined nodes. $QUERY can start at the point determinedby the subscript specified for reference. It proceeds along both the horizontal and verticalaxes. For example:SET exam=$QUERY(^client(4,1,2))$QUERYBased on this example, $QUERY might return any of the following values, assuming a threelevelarray:<strong>Caché</strong> <strong>ObjectScript</strong> <strong>Reference</strong> 291

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

Saved successfully!

Ooh no, something went wrong!