l-gr-21
l-gr-21

Reputation: 31

how to use value of a variable when declaring a tcl list

Is it possible to use variable in tcl list declaration? I have the following simple example:

set grape_color "green"

set produce_list {\
   { PRODUCE COLOR             REGION  }\
   { APPLE   "red"             TX }\
   { GRAPE   $grape_color      CA }\
}

puts $produce_list

The output of the code is this:

{ PRODUCE COLOR             REGION  } 
{ APPLE   "red"             TX }
{ GRAPE   $grape_color      CA }

but I want "green" instead of $grape_color in the list.

Upvotes: 1

Views: 457

Answers (1)

rob mayoff
rob mayoff

Reputation: 386038

One way you can do it is by using the list command.

set grape_color green
set produce_list [list \
    [list PRODUCE COLOR        REGION] \
    [list APPLE   red          TX] \
    [list GRAPE   $grape_color CA]]

Another way is by using the subst command.

set grape_color green
set produce_list {
    { PRODUCE COLOR        REGION }
    { APPLE   red          TX }
    { GRAPE   $grape_color CA }
}
set produce_list [subst -nobackslashes -nocommands $produce_list]

Upvotes: 5

Related Questions