Skip to main content

Nested $Order Loops

Using a series of nested $Order loops (one for each level within a branch of the tree) allows you to reach the leaves, which are the ID numbers. You then use the ID to go back to the data array and retrieve whatever information you desire. Here's an example, using the name index. For each last name, a new loop is called that will find all first names for that last name. And for each full name, a new loop finds all the ID numbers for that name. The ID is used to get the rest of the data from ^PersonD.

SAMPLES>do ^nameloop

Agee,Tommie    617-333-3333   08/09/42
Jones,Cleon    111-111-1111   08/04/42
Swoboda,Ron    222-222-2222   06/08/44
SAMPLES>

The nameloop.mac code:

nameloop ; loop through the name index
    set ln = ""
    For {
        set ln = $order( ^PersonI("Name", ln) )
        quit:(ln = "")
        set fn = ""
        for {
            set fn = $order( ^PersonI("Name", ln, fn) )
            quit:(fn = "")
            set id = ""
            for {
                set id = $order( ^PersonI("Name", ln, fn, id) )
                quit:(id = "")
                set rec = ^PersonD(id)
                write !, $piece( rec, "^", 1), ?15, $piece( rec, "^", 2),
                    ?30, $zdate( $piece( rec, "^", 3) )
                }
            }
        }
FeedbackOpens in a new tab