我有一個數組名爲 「test」:我怎樣才能基於它的鍵排序TCL關聯數組?
key -> value
a 4
f 5
c 3
b 0
d 9
而且我想作爲對數組進行排序:
a 4
b 0
c 3
d 9
f 5
我試着使用:
set sorted_array [lsort [array names test]]
但這隻能返回我:abcdf 我怎樣才能得到整個數組的值(也按照鍵排序)?
我能夠得到排序結果。現在,我試圖把它作爲一個名爲「sorted_array」使用代碼排序後的數組給出如下:
foreach idx [lsort [array names test]] {
append result "$idx $test($idx)"
set sorted_array($idx) $test($idx)
}
現在,當我打印陣列「sorted_array」使用:
foreach index [array names sorted_array] {
puts "sorted_array($index): $sorted_array($index)"
}
但是,我得到相同數組作爲「測試」,而不是一個排序的。
的'parray' PROC做到這一點:在交互式會話,做'粒子陣列env'(加載PROC),然後'信息身體parray' –
Tcl數組不記得按「廣告訂單」(字典做,但它們在舊版本的Tcl中不可用)。此外,它是一個關聯數組,而關聯數組沒有內在排序(它是一組鍵 - 值對)。排序只在你想輸出數組的時候有意義, –