Skip to main content

$Order Loop

generated description: personi

From the example on the previous page, you can see that traversing a tree involves using $Order repeatedly. Let's use a loop to make it easier, and traverse another level of the tree. The code in the example below is so common in ObjectScript that you should study it closely.

It starts by initializing a variable, ln, to the empty string. Then, the For loop begins. The Set statement uses $Order to produce the subscript value of the first child of the ^PersonI("Name") node, a last name. Next, the Quit command checks to see if the loop should end, looking for a last name equal to the empty string. Finally, the code does something with the last name, in this case simply writing it. And then the loop repeats itself, retrieving the next last name. Notice how the code doesn't produce the phone numbers, even though they are “next to” the names, because the phone numbers are not siblings of the names.

The Set statement using $Order is similar to typical programming statements like Set i=i+1, using the current value of a variable to produce a new value.

SAMPLES>d ^simpleloop

Agee
Jones
Swoboda
SAMPLES>

The simpleloop.mac code:

simpleloop ; loop through last names
    set ln = ""
    for {
        set ln = $order( ^PersonI("Name", ln) )
        quit:(ln = "")
        write !, ln
        }
FeedbackOpens in a new tab