问题
set B {pc_0::!mx_0 pi::$mx_0}
puts $B
set A ""
foreach x $B {
lappend A $x
}
puts $A
The output of this program is
pc_0::!mx_0 pi::$mx_0
pc_0::!mx_0 {pi::$mx_0}
It is strange that tcl adds curly braces in second output. I guess it is because it uses $
symbol. But I really need to use it and I don't want the braces to be inserted. How this can be explained and how to avoid the braces?
回答1:
As a general rule, don't treat lists as strings. Pretend that they don't have a string representation. (The string representation is only useful for serialization, debugging, but not for the user).
To convert text (especially user-input) to a list use split.
To convert it back, use join.
Sou you want:
puts [join $A]
Background:
A list have the sideeffect of escaping all meta-characters used by Tcl so no further subsitution takes place when you eval this list. This is a very important property for generating Callbacks/code that will be later executed:
set userinput [gets stdin]
set code [list puts $userinput]
eval $code
No matter what the user enters here, the output is always the same as the user entered, without any substitution.
If the $
would not be escaped, then an evaluation would try to substitute $mx_0
, which will most likly fail.
回答2:
Why not print the list in a way similar to how it was created?
As an experienced programmer new to Tcl, this seems much more intuitive to me:
foreach x $A {
puts $x
}
来源:https://stackoverflow.com/questions/19020505/tcl-adds-curly-braces-when-using-sign