将数组传递给 tcl 中的函数。只有upvar吗?
据我了解,在 tcl 中,如果要将命名数组传递给函数,则必须通过被调用者体内的 upvar 命令访问调用者的上层作用域。这是在 tcl 中传递数组的唯一方法吗?
As far as I understand, in tcl if you want to pass a named array to a function, you have to access the upper scope of the caller via the upvar
command within the callee body. Is this the only way to pass an array in tcl ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如迈克尔指出的,有多种方法,还有一个讨论它的维基页面。只是为了在这里获得一些信息,一些选项是:
By Upvar
By array get/set
As Michael indicated, there are several ways, plus a wiki page that discusses it. Just to have some of that information here, some options are:
By Upvar
By array get/set
还有其他方法,比如先将其转换为列表(通过
array get
和数组集
)。There are other ways, like converting it into a list first (via
array get
andarray set
).如果您只传入数组的值,则可以传入字典(提示:
array get
将数组序列化为字典值)并使用dict
命令访问其中的值。但如果您想访问实时值,upvar
绝对是最简单的。这也是一种非常快的技术;在upvar
本身完成后,它会在变量访问期间编译为指针的额外遍历。If you're only passing in the value of the array, you could pass in a dictionary instead (hint:
array get
serializes an array into a dictionary value) and use thedict
command to access values in it. But if you want access to the live value,upvar
is definitely easiest. It's also a very fast technique; it compiles down to an extra traversal of a pointer during variable access afterupvar
itself finishes.