$Order ループ
前ページの例では、$Order を繰り返し使用して、ツリーを検索しました。ループを使用するとより簡単に取得でき、ツリーの他のレベル (姓) の検索もできます。
以下の例では、2 つの異なる方法で同じ動作を実現しています。コード内のコメントは、各ステップを説明しています。データベースに存在するのは 4 人ですが、ループで生成されるのは 3 つの一意の姓だけであることに注意してください。理由は、ツリー内のノードを図で表すとわかります。また、電話番号のノードは姓のノードとは兄弟でないため、姓の直後に電話番号が存在しても、ループで電話番号は生成されません。
$Order を使用するループ内の Set 文は、典型的なプログラミング・コード set i = i + 1 と類似しています。このコードは、変数値の現在の値を使用して新規の値を生成します。
VS Code - ObjectScript
/// examples for ObjectScript Tutorial
Class ObjectScript.Examples
{
/// loop through last names of the ^PersonI global, 2 different ways
ClassMethod SimpleLoop()
{
write !, "Using argumentless For"
set ln = "" // initialize to the empty string to make $Order return the first last name
for { // start looping
set ln = $order(^PersonI("Name", ln)) // use the current last name to get the next
quit:(ln = "") // stop looping when ln becomes empty again
write !, ?5, ln
}
write !!, "Using While"
set ln = $order(^PersonI("Name", "")) // get the first last name
while (ln '= "") { // only loop if there is at least one last name
write !, ?5, ln
set ln = $order(^PersonI("Name", ln)) // use the current last name to get the next
}
}
}
ターミナルを使用したテスト
USER>do ##class(ObjectScript.Examples).SimpleLoop()
Using argumentless For
Agee
Jones
Swoboda
Using While
Agee
Jones
Swoboda
USER>