Telepresence
Telepresence

Reputation: 629

Create a list where some members are the values of defined variables

I have declared a variable named "_numCols" and set to some integer value (i.e 10). I am trying to include this variable in a list named "arry_in" in following way:

set arry_in {-1 1 $_numCols $_numCols}

But when I display the value of "arry_in" at index 2 and 3, it shows $_numCols instead of 10. Shouldn't $ sign give me the value of _numCols?

Upvotes: 1

Views: 36

Answers (1)

Peter Lewerin
Peter Lewerin

Reputation: 13252

The braces prevent substitution of the content in between them (it's similar to single quotes in shell languages).

Try one of these instead:

set arry_in [list -1 1 $_numCols $_numCols]
set arry_in "-1 1 $_numCols $_numCols"

Double quotes will do in many situations, but will collapse list content which is substituted into the string:

% set x [list foo [list bar baz]]
foo {bar baz}

but

% set x "foo [list bar baz]"
foo bar baz

Sometimes you want one, sometimes the other.

Documentation: Summary of Tcl language syntax

Upvotes: 1

Related Questions