Hashtable
The Hashtable class basically is an additional convenience interface for
treating the variable table of an object like a Hashtable (as for instance
in Java). A Hashtable can be created using Hashtable create ... or it can
be added to an object using classes. Both, the ordinary methods
of Object, such as get and set can be applied on a hashtable, as well
as the hashtable-specific methods explained below. All these methods operate on
the same data: the object's variable table (which is internally implemented
as a hashtable).
The following example creates a hashtable object h1 and initializes it
with setList. Then one variable c is unset and another variable tt is
set, using the ordinary set and unset methods from object. Finally,
the contents of the modified Hashtable are printed using getList.
set h1 [Hashtable create -setList {a b c d r {1 2 3 4} v {7 8}}]
$h1 unset c
$h1 set tt xx
puts "hashtable content: [$h1 getList]"
You can add the hashtable to each existing object or class (as a class, superclass
or mixin),
to benefit from its additional methods:
Object create MyClass
set mc [MyClass create]
$mc classes [$mc getClasses] Hashtable
# now hashtable method can be used
$mc setList {a 1 b 2}
puts "hashtable content: [$mc getList]"
# remove hashtable again
$mc classes MyClass
# the invocation "$mc setList {a 1 b 2}" would raise an error now
<hashtableInstance> clear
clear removes all elements from the hashtable.
The following prints Keys = because h1 is cleared completely.
set h1 [Hashtable create h1 -setList {a b c d}]
$h1 clear
puts "Keys = [$h1 keys]"
<hashtableInstance> containsKey <key>
containsKey returns true, if a hashtable contains the given key
argument, or false if not.
The following code checks for two keys in a hashtable and prints:
Contains a? true, contains x? false.
set h [Hashtable create -setList {a b c d}]
puts "Contains a? [$h containsKey a], contains x? [$h containsKey x]"
<hashtableInstance> getList
getList returns the contents of the complete hashtable as a key/value
pair list.
The following code initializes a hashtable, does a few modifications to the
data, and then uses getList to print the contents. The result is:
Contents: x 2 v {7 8} r {1 2 3 4} c dset h [Hashtable create -setList {a b c d r {1 2 3 4} v {7 8}}]
$h set x 2
$h unset a
puts "Contents: [$h getList]"
<hashtableInstance> keys
keys returns a list of all "keys" in the variable hashtable.
The following lists the keys in the hashtable and prints Keys = a c.
set h1 [Hashtable create h1 -setList {a b c d}]
puts "Keys = [$h1 keys]"
<hashtableInstance> setList <key value pair list>
setList fills the variable hashtable with the data from the key/value
pair list given as an argument. The key/value pair list must have an even
number of list elements.
The following code initializes the variable hashtable via a list and prints
the results. setList overwrites existing content, but does not delete the
contents of the hashtable before writing. Hence, the result of the following
code is: Contents: x d a c v {7 8} r {1 2 3 4} c dset h [Hashtable create -setList {a b c d r {1 2 3 4} v {7 8}}]
$h setList {a c x d}
puts "Contents: [$h getList]"
<hashtableInstance> size
size returns the number of the elements in the hashtable as an integer.
The following code initializes the hashtable with 4 key/value pairs, then
adds another element, and then unsets an element. So the result is a size
of 4 elements: Size: 4.
set h [Hashtable create -setList {a b c d r {1 2 3 4} v {7 8}}]
$h set x 2
$h unset a
puts "Size: [$h size]"
<hashtableInstance> values
values prints all values in the hashtable.
The result of the following code is: Values: 2 {7 8} {1 2 3 4} dset h [Hashtable create -setList {a b c d r {1 2 3 4} v {7 8}}]
$h set x 2
$h unset a
puts "Values: [$h values]"