A dictionary associates keys with values. Keys and values can have any type, which makes dictionaries more flexible than vectors at the cost of slightly slower access.
dictionary(string, money) convertFromUSD; convertFromUSD['EUR'] := 1.272d; convertFromUSD['GBP'] := 1.478d; convertFromUSD['CAD'] := 0.822d;Only one value per distinct key can be held, so this statement overwrites the previous value associated with the key "EUR":
convertFromUSD['EUR'] := 1.275d;
The expression convertFromUSD['CAD'] extracts the value from the dictionary. If there is no matching key, as in convertFromUSD['JPY'], the expression returns null.
You can use the function remove to remove a key and its value from a dictionary. For instance, remove(convertFromUSD,'EUR') removes the key and corresponding value for Euros. The function clear removes all keys from the dictionary. You can test whether the dictionary has no more keys with the empty operation.
for (currency in convertFromUSD) { if (convertFromUSD[currency] > 1) { print('currency ', currency, ' is worth more than one USD.\n'); } }The variable currency, whose scope is restricted to the loop body, has the type of keys of the dictionary (string in this case).
convertFromUSD := new dictionary(string, money);