Dictionary tcl:嵌套字典上的dict筛选器
我有这样一个词汇定义:Dictionary tcl:嵌套字典上的dict筛选器,dictionary,tcl,proc,Dictionary,Tcl,Proc,我有这样一个词汇定义: dict set lag_in lagId 1 dict set lag_in m1 port "1/2" dict set lag_in m1 ixPort $ix_port(1) dict set lag_in m2 port "1/20" dict set lag_in m2 ixP
dict set lag_in lagId 1
dict set lag_in m1 port "1/2"
dict set lag_in m1 ixPort $ix_port(1)
dict set lag_in m2 port "1/20"
dict set lag_in m2 ixPort $ix_port(2)
dict set lag_in m3 port "1/17"
dict set lag_in m3 ixPort $ix_port(17)
dict set lag_in itf1 vlan 1
dict set lag_in itf1 ip 10.0.0.1/24
dict set lag_in itf1 vlan 20
dict set lag_in itf1 ip 10.1.1.20/21
return [dict filter [dict filter $dictionnary key $key] key $key]
我想定义一个根据端口或ixPort返回滞后成员的过程。第一次尝试:
proc getLagMember { dictionnary args } {
set opts(port) ""
getopt opts $args
dict for { key val } $dictionnary {
if { [regexp {m[1-4]} $key] } {
if { [dict get $dictionnary $key port] == $opts(port) } {
return [dict filter $dictionnary key $key]
}
}
}
}
但这并不是我想要的:
% getLagMember $lag_in -port "1/2"
m1 {port 1/2 ixPort {1 4 1}} ;# I am expecting port 1/2 ixPort {1 4 1}
因此我尝试修改返回值
,如下所示:
dict set lag_in lagId 1
dict set lag_in m1 port "1/2"
dict set lag_in m1 ixPort $ix_port(1)
dict set lag_in m2 port "1/20"
dict set lag_in m2 ixPort $ix_port(2)
dict set lag_in m3 port "1/17"
dict set lag_in m3 ixPort $ix_port(17)
dict set lag_in itf1 vlan 1
dict set lag_in itf1 ip 10.0.0.1/24
dict set lag_in itf1 vlan 20
dict set lag_in itf1 ip 10.1.1.20/21
return [dict filter [dict filter $dictionnary key $key] key $key]
但我仍然没有我想要的。我做错了什么?
有没有更好的方法来实现我的目标
编辑:多亏了Glenn,我才有了这个程序,但它不适用于-ixPort选项
proc getLagMember { dictionnary args } {
set opts(port) ""
set opts(ixPort) ""
getopt opts $args
parray opts
dict for { key val } $dictionnary {
if {[catch {dict get $val port} port] == 0 && $port eq $opts(port) || [catch {dict get $val ixPort} ixPort] == 0 && $ixPort eq $opts(ixPort)} {
return $val
}
}
}
带-端口的输出:
% getLagMember $lag_in -port 1/2
opts(ixPort) =
opts(port) = 1/1
port 1/1 ixPort {1 4 1}
带有-ixPort的输出
% getLagMember $lag_in -ixPort {1 4 1}
opts(ixPort) = 1 4 1
opts(port) =
ixPort {1 4 1}
试一试
更新:我没有得到与您看到的相同的错误结果:
tclsh << 'ENDSCRIPT'
array set ix_port {1 {1 4 1} 2 2 17 17}
dict set lag_in lagId 1
dict set lag_in m1 port "1/2"
dict set lag_in m1 ixPort $ix_port(1)
dict set lag_in m2 port "1/20"
dict set lag_in m2 ixPort $ix_port(2)
dict set lag_in m3 port "1/17"
dict set lag_in m3 ixPort $ix_port(17)
dict set lag_in itf1 vlan 1
dict set lag_in itf1 ip 10.0.0.1/24
dict set lag_in itf1 vlan 20
dict set lag_in itf1 ip 10.1.1.20/21
proc checkAttribute {dict attr} {
upvar 1 opts opts
expr {[catch {dict get $dict $attr} value] == 0 && $value eq $opts(-$attr)}
}
proc getLagMember { dictionnary args } {
array set opts {-port "" -ixPort ""}
array set opts $args
dict for { key val } $dictionnary {
if {[checkAttribute $val port] || [checkAttribute $val ixPort]} {
return $val
}
}
}
puts [getLagMember $lag_in -port 1/2]
puts [getLagMember $lag_in -ixPort {1 4 1}]
ENDSCRIPT
喂,你没有这么做,是吗
getLagMember $lag_in -ixPort 1 4 1
我刚刚注意到这不适用于ixPort键。我刚刚将
if
替换为:if{[catch{dict get$val-ixPort}ixPort]==0&&$ixPort eq$opts(ixPort)}{
当我使用选项-ixPort
使用ixPort{1 4 1}时,它返回ixPort{1 4 1}
而不是端口1/2 ixPort{1 4 1}
使用连字符即使使用连字符,我也没有我所期望的。我没有你的getopt
proc.Addparray opts
在你调用后,看看里面有什么。谢谢你的回答。我复制/粘贴了你的脚本并有不同的输出。第一行是正确的端口1/2 ixPort{1 4 1}
,但第二行是ixPort{1 4 1}
。这可能是因为我使用了tcl 8.4?