2016-11-09 166 views
0

我正在運行已嵌入到另一個系統中的tcl 8.5的化身。 該系統將接受並正確地執行命令,例如:tcl中的變量替換

uniformDist minAndMax {1 10} 

其中uniformDist是一些對象,而我沒有內部的知名度。

我希望能這樣說:

set M 1000 
uniformDist minAndMax {1 M} 

但這不起作用。 也不 集合M 1000 uniformDist minAndMax {1 $ M}

我想:

u minAndMax {1 [eval $M]} 

u minAndMax {1 [eval M]} 

無論這些作品。

的錯誤信息是:

expected a real value: unable to convert from: "$M"Error: expected fewer arguments [Error while parsing pair]

expected a real value: unable to convert from: "[eval"Error: expected fewer arguments [Error while parsing pair]

什麼是TCL做的正確方法?

回答

3

變量替換使用$和變量的名稱(例如$M${M})表示。

這不起作用:

uniformDist minAndMax {1 $M} 

因爲括號防止替代:$M只是(分)字符串 '元,大寫字母M'。

這工作:

uniformDist minAndMax [list 1 $M] 

因爲參數列表將列表{1 1000}返回並傳遞給uniformDist之前評估。

表格"1 $M"也可以工作,並且可以使用命令替換[set M]代替上面的變量替換。

文檔: Summary of Tcl language syntax

+0

這就是它!謝謝! – elbillaf

+1

還有使用''1 $ M''和'[subst {1 $ M}]',但'list'命令對複雜的替代品有最少的驚喜。 –

+0

'「1 $ M」'在文本中已經:) –