How to concisely concatenate strings in Tcl?

荒凉一梦 提交于 2019-12-03 16:16:29

问题


I can easily concatenate two variables, foo and bar, as follows in Tcl: "${foo}${bar}".

However, if I don't want to put an intermediate result into a variable, how can I easily concatenate the results of calling some proc?

Long hand this would be written:

set foo [myFoo $arg]
set bar [myBar $arg]
set result "${foo}${bar}"

Is there some way to create result without introducing the temporary variables foo and bar?

Doing this is incorrect for my purposes:

concat [myFoo $arg] [myBar $arg]

as it introduces a space between the two results (for list purposes) if one does not exist.

Seems like 'string concat' would be what I want, but it does not appear to be in my version of Tcl interpreter.

string concat [myFoo $arg] [myBar $arg]

String concat is written about here:

  • http://wiki.tcl.tk/16206

回答1:


You can embed commands within a double-quoted string without the need for a temporary variable:

set result "[myFoo $arg][myBar $arg]"



回答2:


If you are doing this many times, in a loop, or separated by some intermediate code, you might also consider:

set result ""
append result [myFoo $arg]
append result [myBar $arg]
append result [myBaz $arg]



回答3:


just write it as a word with no extra spaces:

[myFoo $arg][myBar $arg]

Tcl sees this as a single word after substitution, regardless of the result of the two subcommands.



来源:https://stackoverflow.com/questions/1430093/how-to-concisely-concatenate-strings-in-tcl

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