{*}在TCL中做什么? [英] What does {*} do in TCL?

查看:87
本文介绍了{*}在TCL中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了一些TCL,但是这种结构使我感到困惑.

I have used some TCL, but this construction stumps me.

当$ res ="Table不存在"时,以下内容将返回什么?

When $res = "Table does not exist", what will the following return?

[list [list {*}$res]]

我知道[list [list $res]]会做什么,但是多余的{*}只会让我感到困惑.

I know what [list [list $res]] would do, but the extra {*} just baffles me.

感谢您的帮助.

推荐答案

当$ res ="Table不存在"时,以下内容将返回什么?

When $res = "Table does not exist", what will the following return?

[list [list {*}$res]]

好吧,首先知道[list {*}…]是一个构造函数,该结构返回省略号中的单词列表(在您的情况下为res变量的内容). 碰巧,在您的情况下,实际效果没有任何意义,因为输入字符串实际上也是格式良好的列表.然后,它成为外部list的单个参数,因此我们得到一个单元素列表,其元素包含按顺序排列的单词Tabledoesnotexist的列表. ,即{Table does not exist}.

Well, first know that [list {*}…] is a construct that returns a list of the words in the ellipsis (the contents of the res variable in your case). It happens that in your case, the net effect is nothing as the input string is actually also a well-formed list. That then becomes a single argument to the outer list and so we get a single-element list as result, whose element contains a list of the words Table, does, not and exist in that order, i.e., {Table does not exist}.

扩展单词形式的列表对于列表的串联很有用; concat命令执行相似的操作(但不完全相同; concat命令涉及一些历史怪异).因此,您将这样连接两个列表:

The list of expanded word form is useful for doing concatenation of lists; the concat command does something similar (but not identical; there are some historical weirdnesses involved in the concat command). Thus, you'd concatenate two lists like this:

set concatenation [list {*}$list1 {*}$list2]

还要注意,扩展(在Tcl 8.5中引入)是 true语法,这在Tcl中是非常不寻常的事情. {*}更改了以下替换的性质,以使其产生多个单词,而不只是一个单词.尽管没有它是可能的,但实际上很难做到正确.例如,如果没有它,则将是:

Also note that expansion (introduced in Tcl 8.5) is true syntax, which is a very unusual thing in Tcl. The {*} changes the nature of the following substitution so that it yields multiple words instead of just one. While it is possible to do without it, it's actually remarkably difficult to get right. For example, without it the above would be:

set concatenation [eval [linsert $list1 0 list] [lrange $list2 0 end]]

扩展的引入极大地减少了大多数Tcl代码中对eval的调用次数(这是一个好处,因为难以正确编写;很多程序员被这一困难所困扰).实践证明,使用exec命令特别有用.它使使用globauto_execok更加容易:

The introduction of expansion has greatly reduced the number of calls to eval required in most Tcl code (a benefit since it was hard to write properly; a lot of programmers were caught out by the difficulty). This has proved particularly beneficial in practice with the exec command; it makes working with glob and auto_execok much easier:

exec {*}[auto_execok $someCmd] $arg1 {*}[glob *.foo]
# Instead of:
#eval [linsert [auto_execok $someCmd] 0 exec] [linsert [glob *.foo] 0 $arg1]
# Or this _wrong_ version:
#eval exec [auto_execok $someCmd] $arg1 [glob *.foo]

U.尽管我知道自己在做什么,但最后一个弯折以非扩展形式写出来有点费力. (错误的版本是错误的,因为如果$arg1包含Tcl元字符,它就会崩溃……)

Ugh. That last one was a bit brain-bending to write out in non-expansion form even though I know what I'm doing. (The wrong version is wrong because it falls apart if $arg1 contains Tcl meta-characters…)

这篇关于{*}在TCL中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆