New key/value pairs may be added to a mapping like this:
This will map key to value in the mapping m. If they key already exists in the mapping, then this will stomp on the previous value with the new value.
Here are some examples:
mapping m; // // m is uninitialized // m = ([ ]); // // m = empty // m["i"] = 1; // // m = "i" -> 1 // m["iv"] = 4; m["v"] = 5; // // m = "i" -> 1 // "iv" -> 4 // "v" -> 5 // m["iv"] = 6; // // m = "i" -> 1 // "iv" -> 6 // "v" -> 5 //
You can also add elements using +=, though this is less efficient because a new mapping gets created internally. However, this does let you add a bunch of things all at once. For example:
Example 5-8. Adding a key/value pair to a mapping using +=
mapping m = ([ ]); m += ([ "i": 1 ]); m += ([ "iv": 4, "v": 5, "ix": 9 ]);
An element of a mapping may be deleted as follows:
For example:
mapping m = ([ ]); m += ([ "i": 1, "iv": 4, "v": 5 ]); // // m = "i" -> 1 // "iv" -> 4 // "v" -> 5 // map_delete(m, "i"); // // m = "iv" -> 4 // "v" -> 5 //
Even though += works, -= does NOT work.