Tcl adds curly braces when using `$` sign

北城以北 提交于 2019-12-02 02:52:44

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.

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!