$Order ループの終了
ここでは、ループの終了方法について学習します。ここで再度、変数 substring には空文字以外の部分文字列が設定されているとします。ここで、quit:($extract(ln, 1, $length(substring)) '= substring) と記述し、“ln の先頭が substring に一致しない場合ループを Quit します”。また、ln = "" がある名前の最後に $Order がアクセスする場合も処理します。つまり、ln が空文字列の場合は ln の $Extract が空文字列で、substring は空文字列でないため、Quit が発生し、ループが終了します。
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
}
}
}
ターミナルを使用したテスト
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>