开发者

How can I use a list in tcl as an argument of multiple strings(not as one string)?

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 Bria开发者_运维问答n 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.


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.


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


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
}


For Tcl 8.4 I'd do it like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜