1
votes
set table [dict create cells state bits state]
set data [dict create 1 "s" 1 "f"]

puts $table
puts $data

puts out:

cells state bits state

1 f

This is quite weird! why doesn't dict create make a full dictionary on 1 s 1 f ? thanks!

ps documentation says:

dict create ?key value ...?
Return a new dictionary that contains each of the key/value mappings listed as arguments (keys and values alternating, with each key being followed by its associated value.) https://www.tcl.tk/man/tcl/TclCmd/dict.htm#M6

Post Post Script: I found that putting any two keys that are the same does the same thing such as:

puts [dict create 2 "s" 2 "f"]

2 f

1

1 Answers

2
votes

If you enter different values for the same key (key = 1 in the first case, key = 2 in the second), an associative container like the Tcl dict will keep only one of those values.

You can still have a data structure that has multiple equal keys, just don't use dict create, which enforces the unique-key trait (that's AFAIK the only reason to initialize dictionaries with dict create):

% set data {1 s 1 f}
1 s 1 f
% dict get $data 1
f
% set data [dict create 1 s 1 f]
1 f
% dict get $data 1
f

Dictionary-handling commands will deal with this structure as if it had only one each of the keys. Dictionary-mutating commands will not preserve the multiple equal keys of the original structure.