如何使用 tcl 中的列表作为多个字符串的参数(而不是作为一个字符串)?
具体来说,我用 : 创建一个列表
for {set i 0} {$i < $val(recv)} {incr i} {
lappend bwList "videoBW_($i).tr"
close $videoBW_($i)
}
,然后我想将该列表作为多个文件的参数,
exec xgraph $bwList -geometry 800x400 &
它会给我错误:
警告:无法打开文件 `videoBW_(0).tr videoBW_(1).tr videoBW_(2).tr'
因为 tcl 将整个列表读取为一个字符串而不是多个字符串。 有没有办法将列表读取为多个字符串?
编辑:
tcl8.4 的解决方案是 Brian Fenton 提供的。 但通过更改 set exec_call {xgraph $bwList -geometry 800x400} 设置 exec_call "xgraph $bwList -geometry 800x400"
基本上,如果您只在 exec 前面添加 eval ,它就可以完成工作。
eval exec xgraph $bwList -geometry 800x400 &
Fot tcl 8.5 Bryan Oakley 提供了一个更优雅的解决方案。
Specifically lets say i create a list with :
for {set i 0} {$i < $val(recv)} {incr i} {
lappend bwList "videoBW_($i).tr"
close $videoBW_($i)
}
and then i want to give that list as an argument of multiple files with
exec xgraph $bwList -geometry 800x400 &
It will give me the error:
Warning: cannot open file `videoBW_(0).tr videoBW_(1).tr videoBW_(2).tr'
Because tcl reads the whole list as one string instead of multiple strings.
Is there a way to read the list as multiple strings ?
EDIT :
The solution for tcl8.4 is the one Brian Fenton provided.
but by changing set exec_call {xgraph $bwList -geometry 800x400}
to set exec_call "xgraph $bwList -geometry 800x400"
Basically if you only add eval in front of exec it does the job .
eval exec xgraph $bwList -geometry 800x400 &
Fot tcl 8.5 Bryan Oakley provided a more elegant solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用 tcl 8.5 或更高版本,您可以执行以下操作:
{*}
称为扩展运算符。在执行语句之前,它将列表扩展为单独的元素。请参阅Tcl 手册页的第 5 节以获得明确的解释。
If you are using tcl 8.5 or later you can do:
{*}
is called the expansion operator. It expands lists into individual elements before the statement is executed.See section 5 of the Tcl man page for the definitive explanation.
在 8.5 及更高版本中:
在 8.4 及之前版本中:
如您所见,8.5 扩展语法有很大帮助,当您从简单的实验到生产代码时尤其如此(例如,如果您添加带有空格的标签参数到该命令行;扩展版本将正确处理它,但使用旧版本时,您将不得不正确引用所有内容,您可以看到上面非常混乱)。
In 8.5 and later:
In 8.4 and before:
As you can see, the 8.5 expansion syntax helps a lot, and that's particularly true as you go from simple experimentation to production code (e.g., if you're adding a label argument with spaces in to that command line; the expansion version will just handle it correctly, but with the old version you'll get into having to quote everything up properly, which you can see is very messy above).
我不完全理解“多个字符串”的意思。如果您展示对 xgraph 的最终调用应该是什么样子,这会对我有所帮助。我认为 xgraph 文件位于末尾(即在 -geometry 800x400 之后)?
无论如何,可能有帮助的是使用eval而不是直接调用exec。
I don't fully understand what you mean by "multiple strings". It would help me if you showed what the final call to xgraph should look like. I thought with xgraph that the files came at the end (i.e. after the -geometry 800x400)?
Anyway what might help is using eval instead of a direct call to exec.
对于 Tcl 8.4 我会这样做:
For Tcl 8.4 I'd do it like this: