Tcl 如何将一个proc中的数组读取到另一个proc,因为proc位于同一命名空间中
在proc show i GET error无法读取数组nettopin中,Tcl 如何将一个proc中的数组读取到另一个proc,因为proc位于同一命名空间中,tcl,Tcl,在proc show i GET error无法读取数组nettopin中,content::display需要: namespace eval ::content { variable nettopin array unset nettopin namespace export display show } proc content::display { } { array unset nettopin set filename "net.txt"
content::display
需要:
namespace eval ::content {
variable nettopin
array unset nettopin
namespace export display show
}
proc content::display { } {
array unset nettopin
set filename "net.txt"
set fh [open $filename r]
while {[gets $fh data] >= 0} {
if {$data ne ""} {
set net [lindex $data 1]
set pin [lindex $data 0]
if { ![info exists nettopin($net)] } {
set nettopin($net) "$pin"
puts $nettopin($net)
} else {
set lastnet $net
set newpin $pin
lappend nettopin($lastnet) $newpin
}
}
}
parray nettopin
}
content::display
#################################
proc content::show {net} {
variable nettopin
array unset nettopin
puts "$nettopin($net)"
}
content::show GNDP
它将使用具有该名称的局部变量,而不是具有该名称的命名空间变量
而且在使用之前立即进行
数组unset nettopin
,这不太可能是个好主意;这会删除数组…为什么要在放入
之前取消数组的设置?不要忽略名称空间eval
中的代码放入变量nettopin
行,这是一个好主意,因为对于这个问题不重要,但它也需要位于使用它的每个过程中。
variable nettopin