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


 
 
1) a (currently) reduced set of functions:
 
HASH: [5] [H.lib, H.new, H.disp, H.get, H.set!]
 
2) building an associative array: {def H key value | ...}
 
{def capitals
 {H.new nyk New York, United States | 
        lon London, United Kingdom |
        par Paris, France |
        mos Moscou, Russia }}
-> capitals
 
3) displaying: {H.disp hash}
{H.disp {capitals}}    
-> 
[
nyk: New York, United States
lon: London, United Kingdom
par: Paris, France
mos: Moscou, Russia
]
 
4) getting a value from a key: {H.get hash key}
 
{H.get {capitals} nyk} -> New York, United States
{H.get {capitals} lon} -> London, United Kingdom
{H.get {capitals} par} -> Paris, France
{H.get {capitals} mos} -> Moscou, Russia
 
5) adding a new (key,value): {H.set! hash key value}
 
{H.disp {H.set! {capitals} bar Barcelona, Catalunya}}
-> 
[
nyk: New York, United States
lon: London, United Kingdom
par: Paris, France
mos: Moscou, Russia
bar: Barcelona, Catalunya
]
 
6) editing a key
 
{H.disp 
  {H.set! {capitals}
          nyk 
          {H.get {capitals} nyk} of America}} // adding "of America" to nyk
-> 
[
nyk: New York, United States of America
lon: London, United Kingdom
par: Paris, France
mos: Moscou, Russia
bar: Barcelona, Catalunya
]
 