Hash Table

A hash is a set of values, with each value associated with a unique key. The value can be retrieved by providing the right key. To create a hash table, put an arrow => in between each key/value pair, and put all pairs inside a pair of braces, separated by commas.
A = {"red" => "maple", "purple" => "lilac", "grey" => "ash"}
Each element of the table can be extracted and reset using the operator @.
>> A = {"red" => "maple", "purple" => "lilac", "grey" => "ash"};
>> A @ "purple"

   lilac

Note that a hash table can also act like a function, therefore to reference the entry of a table A with key r, one can use either A@r, or A(r).

If one needs to create a finite function (whose domain is finite set), one doesn't have to write a bunch of code and can use a hash table instead.

>> f = {1 => 0, 2 => 0, 3 => 0, 4 => 1, 5 => 1, 6 => 1};
>> f(1)
      0
>> f(3)
      0
>> f(6)
      1

Values can be reset or added to the table using

A@r = new_value

Note that although both A@r and A(r) refer to the same value of the table, only A@r can be used as an lvalue in an assignment expression.

A hash table can have a default key, which matches anything that is not explicitly used as a key value. For example, if a table is created by

>> u = {1 => 2, 2 => 3, 3 => 4, 4 => 5, default => 0};
Then u(x) = 0 for any x except 1, 2, 3, 4.

oz 2009-12-22