Name

hash — Create and manipulate hash tables.

Synopsis

hash list
hget hash key
hset hash key value
hcontains hash key
hclear hash
hkeys hash
hremove hash key

Description

The hash command takes an even-numbered list and creates a hash table from it, using the even elements as keys, and odd elements as values. A new hash table is returned. The hget and hset commands operate on hash tables. Both take a hash table as their first argument. hget also takes a key, and returns the corresponding value, or an error if no key by that name exists. To determine whether a given key exists, use the hcontains command, which returns true or false depending on whether the key exists in the hash table.

The hkeys command returns the keys of the hash table, as a list.

The hclear command clears an entire hash table, whereas hremove removes the value associated with a given key.

Example

set foo [hash {a b c d}]
hset $foo a 42
puts [hget $foo a]
	  

Produces:

42