I am trying to substitute a variable in a list:
% set a 1
1
% set b {1 $a}
1 $a
%
% set a 1
1
% set b [list 1 $a]
1 1
When I use the curly braces {}
, to define the list the variable 'a' doesn't get substituted with its value but in case of using list
it does.
How do I substitute a variable's value when using a list defined with {}
?
% set a 1
1
% subst {1 $a}
1 1
% set b [subst {1 $a}]
1 1
But most Tcl:ers actually prefer to use list
when creating a list value with substitutions, like in your second try:
% set b [list 1 $a]
One reason for this is that subst
won't preserve list structure in the value:
% set a {a b}
a b
% set b [subst {1 $a}]
1 a b
% set b [list 1 $a]
1 {a b}
Another possibility is to use double quotes instead of braces; this also won't preserve list structure.
% set b "1 $a"
1 a b
Documentation: list, set, subst, Summary of Tcl language syntax