如何将变量作为dict的关键路径传入tcl?

时间:2016-11-08 01:11:00

标签: dictionary tcl

我有以下'多维'字典

dict set atm_prms bb N [dict create volume 13.3 radius 1.65 vdw_nrg -1.0906 ]
dict set atm_prms bb CA [dict create volume 9.4 radius 1.8 vdw_nrg -0.7708 ]
dict set atm_prms bb C [dict create volume 9.82 radius 1.4 vdw_nrg -0.8052 ]
dict set atm_prms bb O [dict create volume 8.2 radius 1.4 vdw_nrg -0.6724 ]
dict set atm_prms bb OXT [dict create volume 8.2 radius 1.4 vdw_nrg -0.6724 ]
dict set atm_prms ALA CB [dict create volume 16.15 radius 1.9 vdw_nrg -1.3243 ]
dict set atm_prms ARG CB [dict create volume 12.77 radius 1.9 vdw_nrg -1.0471 ]
dict set atm_prms ARG CG [dict create volume 12.77 radius 1.9 vdw_nrg -1.0471 ]
dict set atm_prms ARG CD [dict create volume 12.77 radius 1.9 vdw_nrg -1.0471 ]
dict set atm_prms ARG NE [dict create volume 9 radius 1.65 vdw_nrg -0.738 ]
dict set atm_prms ARG CZ [dict create volume 6.95 radius 1.4 vdw_nrg -0.5699 ]
dict set atm_prms ARG NH1 [dict create volume 9 radius 1.65 vdw_nrg -0.738 ]
dict set atm_prms ARG NH2 [dict create volume 9 radius 1.65 vdw_nrg -0.738 ]
dict set atm_prms ASN CB [dict create volume 12.77 radius 1.9 vdw_nrg -1.0471 ]
dict set atm_prms ASN CG [dict create volume 9.82 radius 1.9 vdw_nrg -0.8052 ]
dict set atm_prms ASN OD1 [dict create volume 8.17 radius 1.4 vdw_nrg -0.6699 ]
dict set atm_prms ASN ND2 [dict create volume 13.25 radius 1.65 vdw_nrg -1.0865 ]

如何使用存储在变量中的密钥路径来检索值?

例如,这有效:

> puts [dict get $atm_prms ALA CB volume]
16.15

但这失败了:

> set my_path {ALA CB volume}
> puts [dict get $atm_prms $my_path]
key "ALA CB volume" not known in dictionary

如何正确使用存储在变量中的密钥路径?

1 个答案:

答案 0 :(得分:3)

因为"ALA CB volume"被替换为dict get命令,因为单个词。您需要告诉Tcl您希望将该列表扩展为单独的单词:

% puts [dict get $atm_prms $my_path]
key "ALA CB volume" not known in dictionary
% puts [dict get $atm_prms {*}$my_path]
16.15

请参阅http://tcl.tk/man/tcl8.6/TclCmd/Tcl.htm

中的规则#5