Maps—also called hashes, dictionaries, and associative arrays—are created by enclosing comma-delimited name-value pairs in curly braces, like so:
some_hash = {"foo" : "bar", "fizz" : {"a" : 5, "b" : 6}, "flop" : [1, 2, 3]};
Maps can be queried in the traditional manner using a key enclosed in curly braces:
my_val = some_hash{"foo"}; // returns "bar"
KRL allows deep queries by what are known as hash paths. A hash path is an array whose elements represent the key values (for a map) or array indices of a path from the root of a complex data structure to the element of interest:
another_val = some_hash{["fizz", "b"]}; // returns 6
There are a number of map operators that affect maps.
For example, to retrieve all values from the map, you use the values()
operator:
my_vals = some_hash.values(); // returns ["bar", {"a" : 5, "b" : 6}, [1, 2, 3]]
As another example, you can also merge hashes using the put()
operator:
new_map = {"foo" : "bar1", "bazz": 4}; some_hash.put(new_map); // returns {"foo" : "bar1", "fizz" : {"a" : 5, "b" : 6}, "flop" : [1, 2, 3], "bazz": 4}
See map operators for additional informaion.
Operations on Maps
There are a number of operators that work on maps. In addition there is an map membership infix operator.