请在下面找到传递数组的代码片段,操作数组,但我无法返回新版本的数组.
这是片段:
proc get_mroute_active { &multicast } {
upvar ${&multicast} MULTICAST ;
set group -1 ;
set src -1 ;
set mcast_group_source_id -1 ;
set MULTICAST($mcast_group_source_id,id) $mcast_group_source_id ;
set MULTICAST($mcast_group_source_id,mcast_group) $group ;
set MULTICAST($mcast_group_source_id,mcast_source) $src ;
puts [array size MULTICAST] ;
parray MULTICAST ;
}
array set multicast { } ;
get_mroute_active [array get multicast] ;
puts [array size multicast] ;
parray multicast ;
Run Code Online (Sandbox Code Playgroud)
并且代码的输出是:
3
MULTICAST(-1,id) = -1
MULTICAST(-1,mcast_group) = -1
MULTICAST(-1,mcast_source) = -1
0
Run Code Online (Sandbox Code Playgroud)
能否帮我们看一下如何将"MULTICAST"变量分配给"多播"?
简短的回答是:您不能从过程返回数组,因为数组不是值 - 它们是命名值的特殊命名集合.
有几种方法可以解决这个问题:
通常的方法是按名称传递数组,并使被调用的过程修改数组.例如,代码
proc foo {arrayName} {
upvar 1 $arrayName ary
incr ary(one)
}
set x(one) 1
foo x
puts $x(one)
Run Code Online (Sandbox Code Playgroud)
将打印"2",因为该过程foo修改了调用者范围中指定数组中的特定值.
注意调用者如何传递数组的名称"x",而不是"它的值"(因为你不能从数组中提取值;但是见下文),然后被调用的过程通过它将一个局部变量绑定到该数组使用upvar命令命名.
另一种方法是使用array get和array set命令从数组中提取键和值,并分别使用键和值填充数组.例如:
set x(one) 1
set x(two) 2
array set another [array get x]
parray another
Run Code Online (Sandbox Code Playgroud)
会打印
another(one) = 1
another(two) = 2
Run Code Online (Sandbox Code Playgroud)
array get给定一个数组的命令返回一个平面列表及其键和它们各自的值交错.这样,您可以从过程返回数组的内容,然后让调用者对这些内容执行任何操作,例如,使用该array set命令在其范围内填充另一个数组(可能与传递给该过程的数组相同)首先).
请注意,它array set具有合并语义:在使用源列表插入/替换其值之前,它不会清空目标数组.
第三种(也可能是最好的)方法是使用字典作为数组的键/值映射,但它们本身就是值,因此它们可以作为其他值自由传递.这需要Tcl 8.5或更高版本(在Tcl 8.4中,您可以以包的形式使用此代码的反向端口.使用dicts,您可以这样:
proc foo d {
dict set d one 2
return $d
}
set x [dict create]
dict set x one 1
set y [foo $x]
puts $y
Run Code Online (Sandbox Code Playgroud)
当程序修改原始字典然后返回它然后调用者将它分配给另一个变量时,将打印"one 2".