Starting Focused $Order Loops
In the two examples of $Order loops (using For and using While), using the empty string for the first call to $Order means “start at the beginning”. Similarly, terminating the loops when ln = "" means “stop at the end”. You can modify either statement to focus this loop to produce fewer subscripts. If you set ln = "B", for example, the last name loop will return “Jones” (skipping “Agee”).
If you want the loops to produce only the names starting with a substring of a name (“Jone”, for example):
Use the substring instead of the empty string for the first call to $Order.
Use a condition that compares the substring with each last name, and terminate the loop once it reaches a name that doesn't start with the substring. There are several ways to do this; the next page shows one approach.
Let's focus the start of the loop. Assume the variable substring holds the substring, and that the substring is not empty. You want to start the $Order loop based on the substring, but you can't simply set ln equal to the substring, because the substring could also happen to be a last name that's already in the database. In that case, the loop will start with the next name, even though there's an exact match for substring 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 substring. Then the loop can proceed and generate the names that follow.
VS Code - ObjectScript
/// examples for ObjectScript Tutorial
/// loop through last names that FOLLOW a substring (including the substring)
read "Search for: ",substring
// find the last name just BEFORE the substring and then start looping
set ln = $order(^PersonI("Name", substring), -1)
set ln = $order(^PersonI("Name", ln))
quit:(ln = "")
write !, ln
Testing using the Terminal
Search for: B
Search for: J
Search for: Jones
Search for: Ju