关联数组与 tcl 中的列表

Associated array with list in tcl

我是 TCL 脚本的初学者,我正在尝试将列表存储为关联数组的一部分,如下所示。

脚本:

set cellno 0
set red redcolor
set green greencolor
set blue bluecolor

set myVariable($cellno) {$red $green $blue}

puts [lindex $myVariable($cellno) 2]

问题:

出于某种原因 puts [lindex $myVariable($cellno) 2] 显示如下值

 $blue

而不是

 bluecolor

这一行:

set myVariable($cellno) {$red $green $blue}

...不替换颜色变量,因为它们在大括号中。您可以使用双引号:

set myVariable($cellno) "$red $green $blue"

由于您使用 lindex 将其用作列表,因此更喜欢 list 以避免无意的分词(以及在空字符串或仅包含空格的变量的情况下合并):

set myVariable($cellno) [list $red $green $blue]