Task

The goal is to create an associative array (also known as a dictionary, map, or hash).



Related tasks:

 Associative arrays/Iteration
 Hash from two arrays


See also

 Array
 Associative array: Creation, Iteration
 Collections
 Compound data type
 Doubly-linked list: Definition, Element definition, Element insertion, List Traversal, Element Removal
 Linked list
 Queue: Definition, Usage
 Set
 Singly-linked list: Element definition, Element insertion, List Traversal, Element Removal
 Stack


import tables
 
var
  hash = initTable[string, int]() # empty hash table
  hash1: Table[string, int]       # empty hash table (implicit initialization).
  hash2 = {"key1": 1, "key2": 2}.toTable # hash table with two keys
  hash3 = [("key1", 1), ("key2", 2)].toTable # hash table from tuple array
  hash4 = @[("key1", 1), ("key2", 2)].toTable # hash table from tuple seq
  value = hash2["key1"]
 
hash["spam"] = 1
hash["eggs"] = 2
hash["foo"] = 3
 
echo "hash has ", hash.len, " elements"
echo "hash has key foo? ", hash.hasKey("foo")
echo "hash has key bar? ", hash.hasKey("bar")
 
echo "iterate pairs:" # iterating over (key, value) pairs
for key, value in hash:
  echo key, ": ", value
 
echo "iterate keys:" # iterating over keys
for key in hash.keys:
  echo key
 
echo "iterate values:" # iterating over values
for key in hash.values:
  echo key