如何用“,”分隔而不是 TCL 列表中的空格?
所以我有一个列表,在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的问题对我来说真的没有意义。在 TCL 中,列表在内部存储为字符串。当你说你想列出{“1”,“2”,“3”}时,我只能假设你指的是列表的外部显示。这可以使用 join 命令来完成,如下所示:
1 2 3
{"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:
1 2 3
{"1",2",3"}
您将列表的字符串表示形式与其内存中的表示形式混淆了。内存中的列表就是一个由其他不相交元素组成的有序列表。如果您需要“漂亮地打印”它(通常用于输出到终端),则从中创建一个字符串,然后输出它。
在您的情况下创建字符串的最简单方法是使用
[join]
,正如已经建议的那样。换句话说,不要被代码
输出“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
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.)