将数组传递给 tcl 中的函数.只有upvar? [英] passing arrays to functions in tcl. Only upvar?
本文介绍了将数组传递给 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 ?
推荐答案
正如 Michael 所指出的,有几种方法,另外还有一个 wiki 页面来讨论它.只是为了在这里获得一些信息,一些选项是:
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:
由 Upvar
proc by_upvar {&arrName} {
upvar 1 ${&arrName} arr
puts arr(mykey)
set arr(myotherkey) 2
}
set myarr(mykey) 1
by_upvar myarr
info exists myarr(myotherkey) => true
- 导致调用者看到对数组的更改
通过数组获取/设置
proc by_getset {agv} {
array set arr $agv
puts arr(mykey)
set arr(myotherkey) 2
return [array get arr]
}
set myarr(mykey) 1
array set mynewarr [by_upvar myarr]
info exists myarr(myotherkey) => false
info exists mynewarr(myotherkey) => true
- 导致调用者看到对数组的更改
- 类似的机制可用于返回数组
这篇关于将数组传递给 tcl 中的函数.只有upvar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文