Maps—also called hashes, dictionaries, and associative arrays—are created by enclosing comma-delimited name-value pairs in curly braces, like so:
...
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:
Code Block | ||||
---|---|---|---|---|
| ||||
another_val = some_hash{["fizz", "b"]}; // returns 6 |
...
Code Block | ||||
---|---|---|---|---|
| ||||
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} |
...
When defining map literals, a string literal must be used to define the keys. This code will not work.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
some_string = "map_key";
literal_map = {some_string : "associated value"}; // DOES NOT WORK |
You must use the put() operator to add string values bound to variables as keys for a map,
For example:
Code Block | ||||
---|---|---|---|---|
| ||||
first_key = "map_key";
second_key = "second_map_key";
new_map = {}.put(first_key, "first entry").put(second_key, "second entry");
/*
New map looks like this:
{
"map_key": "first entry",
"second_map_key": "second entry"
}
*/ |
Operations on Maps
There are a number of operators that work on maps. In addition there is an map membership infix operator.