Skip to main content

Starting Focused $Order Loops

Let's focus the start of the loop. Assume the variable sub holds the substring, and that the substring is not empty. Instead of setting ln to "", as we did in the previous example, we want to start the $Order loop based on the substring. But we can't simply set ln equal to the substring. What if sub equals a last name that's already in the database? Then the loop will start with the next name, even though there's an exact match for sub in the index, because of the initial $Order.

To deal with this, use $Order once in reverse, to set ln equal to the last name preceding sub. Then the loop can proceed and generate the names that follow. But we still need to focus the end of the loop so that we don't get “Swoboda”.

SAMPLES>do ^loopstart
Search for: J

The loopstart.mac code:

loopstart ; loop through last names
    read "Search for: ",sub
    set ln = $order( ^PersonI("Name", sub), -1)
    for {
        set ln = $order( ^PersonI("Name", ln) )
        quit:(ln = "")
        write !, ln
FeedbackOpens in a new tab