**Substitution without evaluation - format, subst** !!!!!! '''[Tcl Tutorial Lesson 33%|%Previous lesson%|%]''' | '''[Tcl Tutorial Index%|%Index%|%]''' | '''[Tcl Tutorial Lesson 38%|%Next lesson%|%]''' !!!!!! The Tcl interpreter does only one substitution pass during command evaluation. Some situations, such as placing the name of a variable in a variable, require two passes through the substitution phase. In this case, the `subst` command is useful. `subst` performs a substitution pass without performing any execution of commands except those required for the substitution to occur, ie: commands within `[]` will be executed, and the results placed in the return string. The example code: ====== puts "[subst $$c]\n" ====== shows an example of placing a variable name in a variable, and evaluating through the indirection. The `format` command can also be used to force some levels of substitution to occur. `subst ?-nobackslashes? ?-nocommands? ?-novariables? string`: Passes `string` through the Tcl substitution phase, and returns the original string with the backslash sequences, commands and variables replaced by their equivalents. If any of the `-no...` arguments are present, then that set of substitutions will not be done. '''Note:''' `subst` does not honor braces or quotes. ---- ***Example*** ====== set a "alpha" set b a puts {a and b with no substitution: $a $$b} puts "a and b with one pass of substitution: $a $$b" puts "a and b with subst in braces: [subst {$a $$b}]" puts "a and b with subst in quotes: [subst "$a $$b"]\n" puts "format with no subst [format {$%s} $b]" puts "format with subst: [subst [format {$%s} $b]]" eval "puts \"eval after format: [format {$%s} $b]\"" set num 0; set cmd "proc tempFileName {} " set cmd [format "%s {global num; incr num;" $cmd] set cmd [format {%s return "/tmp/TMP.%s.$num"} $cmd [pid] ] set cmd [format "%s }" $cmd ] eval $cmd puts "[info body tempFileName]" set a arrayname set b index set c newvalue eval [format "set %s(%s) %s" $a $b $c] puts "Index: $b of $a was set to: $arrayname(index)" ====== <> Resulting output ======none a and b with no substitution: $a $$b a and b with one pass of substitution: alpha $a a and b with subst in braces: alpha $a a and b with subst in quotes: alpha alpha format with no subst $a format with subst: alpha eval after format: alpha global num; incr num; return "/tmp/TMP.11168.$num" Index: index of arrayname was set to: newvalue ====== <> !!!!!! '''[Tcl Tutorial Lesson 33%|%Previous lesson%|%]''' | '''[Tcl Tutorial Index%|%Index%|%]''' | '''[Tcl Tutorial Lesson 38%|%Next lesson%|%]''' !!!!!!