如何在Tcl中简洁地连接字符串?

发布于 2024-08-05 01:22:07 字数 680 浏览 5 评论 0 原文

我可以轻松连接两个变量 foo 和 bar,如 Tcl 中所示:“${foo}${bar}”。

但是,如果我不想将中间结果放入变量中,如何轻松连接调用某个过程的结果?

长手这将被写成:

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

有没有某种方法可以在不引入临时变量 foo 和 bar 的情况下创建结果?

这样做对于我的目的来说是不正确的:

concat [myFoo $arg] [myBar $arg]

因为如果两个结果不存在,它会在两个结果之间引入一个空格(用于列表目的)。

看起来“字符串连接”就是我想要的,但它似乎不在我的 Tcl 解释器版本中。

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

字符串连接写在这里:

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:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

想念有你 2024-08-12 01:22:07

您可以将命令嵌入双引号字符串中,而不需要临时变量:

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

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

set result "[myFoo $arg][myBar $arg]"
孤单情人 2024-08-12 01:22:07

如果您在循环中多次执行此操作,或由某些中间代码分隔,您还可以考虑:

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

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]
谷夏 2024-08-12 01:22:07

只需将其写为没有多余空格的单词:

[myFoo $arg][myBar $arg]

Tcl 在替换后将其视为单个单词,无论两个子命令的结果如何。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文