Changing a value inside nested collections

Syntax and values

A list can contain maps, and a map can contain lists. Follow a path through these collections to change one value, like finding a card inside a box inside a cupboard.

In players[0].stats.score, the root is the starting name players. [0] selects the first list item; .stats and .score select named map entries. Those added parts are called suffixes. The root must be a name you may update, and every place or key along the path must already exist. set changes the final value; it does not create missing places.

Index and member suffixes can be combined when the root is assignable:

# language: en
let players = [
    {"name": "Pip", "stats": {"score": 0, "lives": 3}},
]

set players[0].stats.score = 100
say players[0].name, players[0].stats.score

Example

# language: en
let players = [{"score": 12}]
set players[0].score = 20
say players[0].score

Syntax and values · Built-ins · Programming guides