Dictionaries
Dictionaries (dicts) are key-value collections where keys are strings.
Creating dicts
Keys must be strings. Values can be any type.
Accessing values
Use bracket notation with a string key:
Accessing a key that does not exist produces a runtime error.
Setting values
If the key exists, its value is updated. If it does not exist, a new entry is added.
Dict methods
| Method | Description | Returns |
|---|---|---|
.keys() |
List of all keys (in insertion order) | list |
.values() |
List of all values | list |
.has(key) |
Check if a key exists | bool |
.length() |
Number of entries | int |
.tostring() |
String representation | string |
var dict d = {"x": 10, "y": 20}
console.println(d.keys().tostring()) // ["x", "y"]
console.println(d.has("x").tostring()) // true
console.println(d.length().tostring()) // 2
Iterating over dicts
A for-in loop over a dict iterates over the keys:
var dict scores = {"alice": 90, "bob": 85}
for key in scores {
console.println(key + " = " + scores[key].tostring())
}
Keys are returned in insertion order.