字符串我有以下字符串:TCL:獲取數出使用掃描
set operating_period "1.86ns" ; # set dominant default period , from create_clock command in sdc
我想獲得多少出於此。所以結果應該是 1.86
任何建議如何在TCL中做到這一點? 我嘗試掃描,但顯然我失敗=(...
字符串我有以下字符串:TCL:獲取數出使用掃描
set operating_period "1.86ns" ; # set dominant default period , from create_clock command in sdc
我想獲得多少出於此。所以結果應該是 1.86
任何建議如何在TCL中做到這一點? 我嘗試掃描,但顯然我失敗=(...
使用掃描:
% set operating_period "1.86ns"
1.86ns
% set x [scan $operating_period %f]
1.86
是的,但數字只是一個例子,我想從這個任意數字 –
工作原理:掃描1ns%f - > 1.0,掃描1.8ns%f - > 1.8 等 – siyb
我覺得你不明白這個問題,如果數字是固定的,我不必編寫代碼來獲取它......只需鍵入它^^。我正在尋找諸如「scan $ string%f」之類的東西,結果是字符串變量中的數字,而字符串看起來像一個以上,只是數字不知道... –
有時,特別是病態的數據時(如,任何人以自由形式寫的任何東西),你必須使用混合技術來提取數據。例如,你可以同時使用regexp
和scan
:
set inputString "wow yet 183.326ns another float"
if {[scan [regexp -inline {[\d.]+ns} $inputString] "%f" value] == 1} {
# Found something! It's in $value now
}
regexp
是否提取(-inline
很好;它使得regexp
返回匹配的內容)和scan
「從所發現的內容中提取出」意義「,並將一個理智的浮點數存儲在$value
中,假設其中有任何存在。您可能需要調整RE才能獲得最佳結果(例如,目前的那個目前無法應付負數)。
沒關係...我做到了! –
lindex [split [lindex [split $ string \「] 1] ns] 0 –
這是非常討厭的,但它的工作原理=)! –