Skip to main content

Nested Traverse Loops

Using a series of nested Traverse() 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.


ln = ""
do
    ln = traverse(^PersonI("Name", ln) )
    if ln = "" then exit do
    fn = ""
    do
        fn = traverse(^PersonI("Name", ln, fn) )
        if fn = "" then exit do
        id = ""
        do
            id = traverse(^PersonI("Name", ln, fn, id) )
            if id = "" then exit do
            rec = ^PersonD(id)
            print piece(rec, "^", 1), space(2), 
            print piece(rec, "^", 2), space(2)
            println DateConvert(piece(rec, "^", 3), vbToExternal)
        loop
    loop
loop

The example code is in the nameloop subroutine of BAStraverse.BAS. Run it using the Terminal.


SAMPLES>do nameloop^BAStraverse()

FeedbackOpens in a new tab