0

I'm beginner in TCL scripting and I'm trying to store list as part of associated array as below.

script:

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

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

puts [lindex $myVariable($cellno) 2]

problem:

For some reason puts [lindex $myVariable($cellno) 2] is displaying value as below

 $blue

Instead of

 bluecolor
2
  • 4
    Braces won't do variable substitution in Tcl. Try with double quotes as set myVariable($cellno) "$red $green $blue" Commented Sep 1, 2020 at 4:29
  • Does this answer your question? List passed to join doesn't produce the correct output Commented Sep 1, 2020 at 4:34

1 Answer 1

3

This line:

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

...does not substitute the color variables since they are in braces. You could use double quotes:

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

Since you use it as a list using lindex, prefer list to avoid unintentional word splitting (and merging in case of empty string or whitespace only variables):

set myVariable($cellno) [list $red $green $blue]
Sign up to request clarification or add additional context in comments.

1 Comment

Using list like that allows Tcl to use a more efficient implementation than using double quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.