2011-01-24 89 views

回答

2

相反阵列,您要使用的词典:http://www.tcl.tk/man/tcl8.5/TclCmd/dict.htm

词典是一流的变量可以被传递(并放置在列表)就像其他变量。

% set d1 [dict create a b c d] 
% set d2 [dict create e f g h i j] 
% set lst [list $d1 $d2] 
% set lst ;# ==> {a b c d} {e f g h i j} 
+0

另外`阵列GET`返回dictionary在8.5 – 2011-01-25 13:15:58

2

在工作中,我们仍然使用Tcl 8.4。我知道字典已被移植,但它不是标准软件包的一部分。对于8.4,我们使用Tclx包中的键控列表。这里是一个例子:

# Problem: I want to create a list of arrays 
# Solution: For 8.5, I can have list of dict, but for 8.4, use 
# keyedlist in place of dict. This script is written for 8.4 

package require Tclx 

# Create individual users and a list 
keylset user1 id 101 alias john; # {{id 101} {alias john}} 
keylset user2 id 102 alias ally; # {{id 102} {alias ally}} 
set users [list $user1 $user2] 

# Show the list 
foreach user $users { 
    puts "ID: [keylget user id]" 
    puts "Alias: [keylget user alias]" 
    puts "" 
} 

输出:

ID: 101 
Alias: john 

ID: 102 
Alias: ally