How I add bracket in string variable in TCL file

末鹿安然 提交于 2020-11-29 21:06:47

问题


I have written one TCL script but I have one problem when making a string variable as below:

set a 100
set b "this is variable[$a]"

I want b to be assign with b = "this is variable[100]" but I got the error:

invalid command name 100

Please help me to fix it :-(.


回答1:


You just need to escape it:

set a 100
set b "this is variable\[$a\]"



回答2:


Other possibilities (but escaping the brackets is better):

set b [format {this is variable[%d]} $a]
set b [subst -nocom -noback {this is variable[$a]}]

Documentation: set, format, subst



来源:https://stackoverflow.com/questions/39974070/how-i-add-bracket-in-string-variable-in-tcl-file

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