Frag Logo
  Frag Home | Frag SF Project   index | contents | previous | next

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

Hashtable clear

Syntax

<hashtableInstance> clear

Description

clear removes all elements from the hashtable.

Example

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]"

Hashtable containsKey

Syntax

<hashtableInstance> containsKey <key>

Description

containsKey returns true, if a hashtable contains the given key argument, or false if not.

Example

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]"        

Hashtable getList

Syntax

<hashtableInstance> getList

Description

getList returns the contents of the complete hashtable as a key/value pair list.

Example

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 d
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 "Contents: [$h getList]"

Hashtable keys

Syntax

<hashtableInstance> keys

Description

keys returns a list of all "keys" in the variable hashtable.

Example

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]"

Hashtable setList

Syntax

<hashtableInstance> setList <key value pair list>

Description

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.

Example

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 d
set 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]"

Hashtable size

Syntax

<hashtableInstance> size

Description

size returns the number of the elements in the hashtable as an integer.

Example

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]"

Hashtable values

Syntax

<hashtableInstance> values

Description

values prints all values in the hashtable.

Example

The result of the following code is: Values: 2 {7 8} {1 2 3 4} d
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 "Values: [$h values]"

  index | contents | previous | next