5.4. Accessing Elements in Mappings

An element of a mapping may be referenced as follows:

Example 5-10. Referencing a key/value pair in a mapping


  mapping m = ([ "i": "1" ]);
  string k = "i";

  write(m["i"] + "\n");

  write(m[k] + "\n"); 

If the key does not exist in the map, or if it existed and you've deleted it using map_delete, then accessing the key will return 0, but that 0 is really an undefined.

For example:


  mapping m = ([ ]);

  m += ([ "o": 0, "i": 1, "iv": 4, "v": 5 ]);

  map_delete(m, "i");

  m["o"];                 // this returns 0
  undefinedp(m["o"]);     // this is false because "o" is a valid
                          // key in mapping m

  m["i"];                 // this returns undefined
  undefinedp(m["i"]);     // this is true because "i" is not a valid
                          // key in mapping m

  m["joe"];               // this returns undefined
  undefinedp(m["joe"]);   // this is true because "joe" is not a valid
                          // key in mapping m