は、expr
それが生成する結果がコマンドに指定されていることを知りません。浮動小数点数が必要なので、両方のオペランドが整数なので、整数除算を使用しています。
我々は何も変更せずに、いくつかの段階で全体のことを行うことができます
set a 3
set b [expr ($a+2)/2]
puts "b=$b"
puts [format "%.3f" $b]
あなたは($a+2)/2
が2
であることを見ることができるように、私は、同様の中間値をプリントアウトしてきました。 Tclの整数部は切り捨てられます。それが真実であるとすれば、format
は2.000
という値をレンダリングすることは不思議ではありません。
2番目の質問では、Tclはマルチレベルbreak
をサポートしていません。しかし、それは問題として非常に頻繁に起きることはなく、ヘルパ・プロシージャの戦略的使用によって処理できるケースの大半は、
詳細には、break
を使用して、現在の最も内側のループを停止することができますが、最も内側のループのみを停止することができます。その間のSecond Looping process
は、最も内側の入れ子レベルのbreak
によって停止されません。直接これに対処するための最も簡単なメカニズムは、カスタム例外コードですが、それはほとんど簡単です:
set MyException 123; # Small integer greater than 5
for {set numA 0} {$numA < 5} {incr numA} { #First Looping process
for {set numB 0} {$numB < $oneNum} {incr numB} { #Second Looping process
try {
for {set numC 0} {$numC < $twoNum} {incr numC} { #Third Looping process
if {$dataA == $dataB} {
return -level 0 -code $MyException
}
}
} on $MyException {} {
break
}
}
}
あなたが代わりにフラグ変数を使用することにより深くトリッキーなカスタム例外コードを避けることができます:
for {set numA 0} {$numA < 5} {incr numA} { #First Looping process
for {set numB 0} {$numB < $oneNum} {incr numB} { #Second Looping process
set foundIt false
for {set numC 0} {$numC < $twoNum} {incr numC} { #Third Looping process
if {$dataA == $dataB} {
# Set the flag to indicate we've found our matching case
set foundIt true
# Terminate the inner loop
break
}
}
if {$foundIt} {
break
}
}
}
このかなり醜いです(特にロジックが複雑になった場合)が、動作します。幸いにも、それは非常に頻繁に上がらない。より頻繁に、人はこのようなコードを分割:
proc innerSearch {numA} {
global oneNum twoNum
for {set numB 0} {$numB < $oneNum} {incr numB} { #Second Looping process
for {set numC 0} {$numC < $twoNum} {incr numC} { #Third Looping process
if {$dataA == $dataB} {
# Stop the Second Looping by returning from the procedure
return
}
}
}
}
for {set numA 0} {$numA < 5} {incr numA} { #First Looping process
innerSearch $numA
}
これは、より多くのニーモニック名が使用される場合には、実際には多くの意味をなす傾向があります。
おかげに行く続けるので、私はまだ理解していけないのです。私に例を挙げてみたいですか? –
ありがとうございます! –