Tcl question: expansion of variable names

Joshua Root jmr at macports.org
Tue Sep 4 23:58:39 UTC 2018


On 2018-9-5 05:45 , Gustaf Neumann wrote:
> On 04.09.18 15:53, Ryan Schmidt wrote:
>> I've never been quite clear on the benefits of using [list] vs just
>> making a string, since a string is a list, and I seem to use them
>> interchangeably.
> 
> in case the interpolated variables have values that have no Tcl meaning,
> one can use
> 
>    set a 10
>    set b 20
>    set l1 "$a $b"
>    set l2 [list $a $b]
> 
> and get the same result from double quotes and [list], when e.g. using
> list operations like
> 
>    puts [lindex $l1 1]
>    puts [lindex $l2 1]
> 
> however, when e.g. variable "a" has a value like "1 2" the output will
> differ,
> since l1 consists now of three blank separates values,
> where l2 keeps the the value a regardless of its contents a properly braced
> as first element of the list. Same happens, when "a" is e.g. empty, etc.

Indeed. To demonstrate:

% set a Foo
Foo
% set b "John Smith"
John Smith
% set l1 "$a $b"
Foo John Smith
% lindex $l1 0
Foo
% lindex $l1 1
John
% set l2 [list $a $b]
Foo {John Smith}
% lindex $l2 0
Foo
% lindex $l2 1
John Smith

- Josh


More information about the macports-dev mailing list