Associative arrays are arrays that map (or associate) a set of keys to a set of values. The data type of the keys need not be an integer, so descriptive strings, for instance, may be used. Keys must be unique, but need not be contiguous, or even ordered.
ages as Int[String]
The code above declares an associative array named ages, which is of type Int and is indexed by string keys.
ages as Int[String] ages = ["John" : 23, "Peter" : 42, "Mary" : 29]
The code above initializes the array ages, associating the value 23 to the key "John", the value 42 to the key "Peter", and the value 29 to the key "Mary".
ages as Int[String] ages["John"] = 23 ages["Peter"] = 42 ages["Mary"] = 29
The code above also initializes the empty array ages, and then adds associated key-value pairs. The value 23 is assigned to the key "John", the value 42 to the key "Peter", and the value 29 to the key "Mary". If a key which already exists in the array is used again with a new value, this value replaces the old value for that key.
ages as Int[String] ages = ["John" : 23, "Peter" : 42, "Mary" : 29] display ages["John"]
If you pass a nonexistent key, a null value is returned.
ages as Int[ordered String]
ages as Int[ordered String]
ages = ["John" : 23, "Peter" : 42, "Mary" : 29]
for key in ages do
display key
end
The keys will be displayed in ascending order, using the key data type sort order, which in this case is alphabetical.