【发布时间】:2014-01-15 20:31:45
【问题描述】:
我有一个这样定义的字典:
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
我想定义一个根据端口或 ixPort 返回滞后成员的 proc。第一次尝试:
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}
所以我尝试像这样修改return:
return [dict filter [dict filter $dictionnary key $key] key $key]
但我仍然没有我想要的东西。我究竟做错了什么 ? 有没有更好的方法来实现我想要的?
编辑:感谢 Glenn,我附带了这个 proc,但它不适用于 -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
}
}
}
使用 -port 输出:
% 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}
【问题讨论】:
标签: dictionary tcl proc