Ending Focused $Order Loops
Now let's focus the end of the loop. Again, assume the variable substring holds the non-empty substring. Then the following statement: quit:($extract(ln, 1, $length(substring)) '= substring) means: “Quit if the beginning of ln doesn't match substring”. It also handles the case where $Order reaches the end of the names, where ln = "". That's because, if ln is the empty string, $Extract of ln is the empty string, and since substring isn't empty, the Quit happens and the loop ends.
VS Code - ObjectScript
Class ObjectScript.Examples
{
/// loop through last names that MATCH substring
ClassMethod FocusedLoopStartEnd()
{
read "Search for: ",substring
// find the last name just BEFORE the substring and then start looping
set ln = $order(^PersonI("Name", substring), -1)
for {
set ln = $order(^PersonI("Name", ln))
// quit if no match or at end
quit:($extract(ln, 1, $length(substring)) '= substring)
write !, ln
}
}
}
Testing using the Terminal
USER>do ##class(ObjectScript.Examples).FocusedLoopStartEnd()
Search for: A
Agee
USER>do ##class(ObjectScript.Examples).FocusedLoopStartEnd()
Search for: Jo
Jones
USER>do ##class(ObjectScript.Examples).FocusedLoopStartEnd()
Search for: Jones
Jones
USER>do ##class(ObjectScript.Examples).FocusedLoopStartEnd()
Search for: Ju
USER>do ##class(ObjectScript.Examples).FocusedLoopStartEnd()
Search for: Sw
Swoboda
USER>