Skip to main content

# Traverse() Function

You finished Part 1 by creating an index, ^PersonI, where most of the data is stored in the subscripts. Now it's time for you to learn how to access data stored in subscripts. Given a node in an array, the Traverse() function gives you access to the values of the subscripts of its children. Because subscripts are automatically sorted, this function traverses the children in order.

To use it, you pass Traverse an array name along with one or more of its subscripts. The array and all but the final, rightmost subscript identify a node. The final subscript is the subscript on which Traverse focuses. In the example using the x array, the subscript on which Traverse focuses is the first and only subscript. Remember that the empty string ("") is not allowed to be a subscript of an array. But it can be used as the subscript of the array passed to Traverse. It causes Traverse to return the first valid subscript.

So Traverse returns the first valid subscript, in this case, 1. Passing 1 as the subscript in turn returns the next valid subscript, 4. Passing 4 returns 9, and passing 9 causes Traverse to return the empty string, signifying that there aren't any more valid subscripts.

``````
x(1)=1 : x(4)=2 : x(9)=3
println "first subscript is: ", traverse( x("") )
println "subscript after 1 is: ", traverse( x(1) )
println "subscript after 4 is: ", traverse( x(4) )
println "subscript after 9 is: ", traverse( x(9) )
``````
FeedbackOpens in a new tab