tcl 中array做为函数参数传递的调用方法

现在一般教本语言(perl python tcl)中都会提供字典(数组)这样的数据结构,
这种结构使用起来最为灵活方便,但是也最不容易掌握。

现在一般教本语言(perl python tcl)中都会提供字典(数组)这样的数据结构,
这种结构使用起来最为灵活方便,但是也最不容易掌握。

在TCL中可以将数组名作为函数参数,再通过upvar,得到数组的内容。
下面的例子就显示了这个用法:

#!/usr/bin/tclsh

proc foo {arr_foo} {
    upvar $arr_foo arr
    puts -nonewline {$arr(key1):}
    puts $arr(key1)
    puts -nonewline {$arr(key2):}
    puts $arr(key2)
}

set a(key1) value1
set a(key2) value2
foo a       

  
结果:

$arr(key1):value1
$arr(key2):value2

Leave a Reply

Your email address will not be published. Required fields are marked *