如何用“,”分隔而不是 TCL 列表中的空格?

发布于 2024-11-27 03:19:17 字数 139 浏览 1 评论 0原文

所以我有一个列表,在 TCL 中,项目存储为 {"1" "2" "3"},以空格分隔。我想将此列表转换为 {"1","2","3"} - 除了通过 foreach 和 lappending 到每个项目的末尾之外,执行此操作的有效方法是什么?

谢谢!

So I've got a list, and in TCL the items are stored as {"1" "2" "3"}, space seperated. I'd like to convert this list to being {"1","2","3"} -- what would be an efficient way of doing this, aside from going through a foreach and lappending , to the end of each item?

Thanks!

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

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

发布评论

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

评论(2

如梦亦如幻 2024-12-04 03:19:17

你的问题对我来说真的没有意义。在 TCL 中,列表在内部存储为字符串。当你说你想列出{“1”,“2”,“3”}时,我只能假设你指的是列表的外部显示。这可以使用 join 命令来完成,如下所示:

% set x [list 1 2 3]

1 2 3

% set  z "\{\"[join $x "\","]\"\}"

{"1",2",3"}

Your question doesn't really make sense to me. In TCL, lists are stored internally as strings. When you say you want to list to {"1","2","3"}, I can only assume you are referring to the external display of the list. That can be done using the join command as follows:

% set x [list 1 2 3]

1 2 3

% set  z "\{\"[join $x "\","]\"\}"

{"1",2",3"}

ゝ偶尔ゞ 2024-12-04 03:19:17

您将列表的字符串表示形式与其内存中的表示形式混淆了。内存中的列表就是一个由其他不相交元素组成的有序列表。如果您需要“漂亮地打印”它(通常用于输出到终端),则从中创建一个字符串,然后输出它。
在您的情况下创建字符串的最简单方法是使用 [join],正如已经建议的那样。

换句话说,不要被代码

set L [list 1 2 3]
puts $L

输出“1 2 3”这一事实所欺骗:这并不意味着“列表存储为字符串,其元素以空格分隔”。当您要求 Tcl 通过将该列表传递给需要字符串值的 [puts]隐式从列表中生成字符串时,它只是 Tcl 使用的列表的默认字符串表示形式。 (NB严格来说,“需要一个字符串值”对于 Tcl 内部来说是不正确的,但现在让我们忽略这一点。)

You're confusing string representation of a list with its in-memory representation. A list in memory is just that--an ordered list of otherwise disjoint elements. If you need to "pretty print" it (for output to a terminal, typically) make a string from it, and output it then.
The simplest way to make a string in your case is to use [join] as was already suggested.

In other words, don't be deceived by the fact that the code

set L [list 1 2 3]
puts $L

outputs "1 2 3": this does not mean that "a list is stored as a string with its elements space-separated". It's just the list's default string representation used by Tcl when you ask it to implicitly make a string out of a list by passing that list to [puts] which expects a string value. (NB strictly speaking, "expects a string value" is not correct with regards to the Tcl internals, but let's ignore this for now.)

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