如何使用 tcl 中的列表作为多个字符串的参数(而不是作为一个字符串)?

发布于 2024-11-06 02:53:54 字数 736 浏览 3 评论 0原文

具体来说,我用 : 创建一个列表

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 技术交流群。

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

发布评论

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

评论(4

只是我以为 2024-11-13 02:53:54

如果您使用 tcl 8.5 或更高版本,您可以执行以下操作:

exec xgraph {*}$bwList -geometry 800x400  &

{*} 称为扩展运算符。在执行语句之前,它将列表扩展为单独的元素。

请参阅Tcl 手册页的第 5 节以获得明确的解释。

If you are using tcl 8.5 or later you can do:

exec xgraph {*}$bwList -geometry 800x400  &

{*} 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.

菊凝晚露 2024-11-13 02:53:54

在 8.5 及更高版本中:

exec xgraph {*}$bwList -geometry 800x400 &

在 8.4 及之前版本中:

eval exec xgraph $bwList -geometry 800x400 &
# Or this more formally-correct version...
# eval [list exec xgraph] [lrange $bwList 0 end] [list -geometry 800x400 &]

如您所见,8.5 扩展语法有很大帮助,当您从简单的实验到生产代码时尤其如此(例如,如果您添加带有空格的标签参数到该命令行;扩展版本将正确处理它,但使用旧版本时,您将不得不正确引用所有内容,您可以看到上面非常混乱)。

In 8.5 and later:

exec xgraph {*}$bwList -geometry 800x400 &

In 8.4 and before:

eval exec xgraph $bwList -geometry 800x400 &
# Or this more formally-correct version...
# eval [list exec xgraph] [lrange $bwList 0 end] [list -geometry 800x400 &]

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).

尐籹人 2024-11-13 02:53:54

我不完全理解“多个字符串”的意思。如果您展示对 xgraph 的最终调用应该是什么样子,这会对我有所帮助。我认为 xgraph 文件位于末尾(即在 -geometry 800x400 之后)?

无论如何,可能有帮助的是使用eval而不是直接调用exec

set exec_call {xgraph $bwList -geometry 800x400}
set caught [catch {eval exec $exec_call} result]
if { $caught } {
 #handle the error - it is stored in $result
} else {
 #handle the success
}

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.

set exec_call {xgraph $bwList -geometry 800x400}
set caught [catch {eval exec $exec_call} result]
if { $caught } {
 #handle the error - it is stored in $result
} else {
 #handle the success
}
鹤仙姿 2024-11-13 02:53:54

对于 Tcl 8.4 我会这样做:

set cmd [concat [list exec xgraph] $bwList]
lappend cmd -geometry 800x400

For Tcl 8.4 I'd do it like this:

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