Example:
GTM>K S (a(1),a(2000),a("CAT"),a("cat"),a("ALF"), a(12))=1 GTM>S x="" F S x=$0(a(x)) U:x="" W !,x 1 12 2000 ALF CAT cat GTMK> a("CAT") SET a(5,10)="woolworths",a("cat")="last" GTM> S x="" F S x=$O(a(x),-1) Q:x="" W !,x cat ALF 2000 12 5 1 GTM>
This example uses a $ORDER() loop to display all the subscripts at the first level of local variable a, make some changes in a, and then display all the subscripts in reverse order. Notice that $ORDER() returns only the existing subscripts in the sparse array and returns them in M collation sequence, regardless of the order in which they were entered. Also, $ORDER() does not differentiate between node A(5), which has only descendants (no data value), and the other nodes, which have data values.
Example:
GTM>k s (%(1),tiva(2),A(3),tiv(4),Q(5),%a(6))="" GTM>s x="%" GTM>w:$d(@x) !,x f s x=$order(@x) q:x="" w !,x % %a A Q tiv tiva x GTM>s x="zzzzzzzz" GTM>w:$d(@x) !,x f s x=$order(@x,-1) q:x="" w !,x x tiva tiv Q A %a % GTM>
This loop uses $ORDER() to display the current local variable names in both forward and reverse order. Notice that the first ([^]%) and last ([^]zzzzzzzz) names require handling as special cases and require a $DATA() function.
Example:
SET acct="",cntt="" FOR SET acct=$OREDER(^acct(acct)) QUIT:acct="" DO . F SET cntt=$ORDER(^acct(acct,cntt)) DO WORK QUIT
This uses two nested $ORDER() loops to cycle through the ^acct global array and perform some action for each second level node.