Using Variables in Associative Arrays

I ran into this problem when I tried to insert a variable into the value part of an associative array(or hash) in Tcl. Apparently, in this situation the variable substitution does not take place...

#The Variable
set ship "Enterprise"

#Make the array
array set star_trek {
	location	"Bridge of the $ship"
	problem		"Power surge"
	solution	"a fuse"

puts $star_trek(location) ;# Prints "Bridge of the $ship"

I think the '{' and '}' around the code prevents the substitution. Yeah, those brackets are very confusing in Tcl. You can get around this error with this code...

#The Variable
set ship "Enterprise"

#Make the array
set star_trek(location)	"Bridge of the $ship"
set star_trek(problem)	"Power surge"
set star_trek(solution)	"a fuse"

puts $star_trek(location) ;# Prints "Bridge of the Enterprise"
blog comments powered by Disqus
Subscribe to Feed