[DGD] n00b question on mappings

Erwin Harte harte at is-here.com
Tue May 22 06:49:37 CEST 2007


Kurt Nordstrom wrote:
> Okay, so, I'm not sure I'm understanding mapping syntax, maybe somebody 
> can help me out.
> 
> Let's say I've got a mapping, "myMap".
> 
> I initialize myMap before I use it, with myMap = ([ ]);
> 
> If I want to add a value to myMap, am I able to simply do myMap["age"] = 
> 18, or do I need to do myMap += (["age" : 18 ])?

Both will work.  The difference is that the first changes the mapping on the 
spot, while the second creates a new one and assigns that to myMap.

This becomes a useful difference if you do something like this:

     void add_data(mapping map)
     {
         map["foo"] = "bar";
     }

Which you can now call like this:

     mapping m;

     m = ([ ]);
     add_data(m);

And at the end of that m contains an entry "foo" with value "bar", which 
wouldn't be the case if add_data looked like this:

     void add_data(mapping map)
     {
         map += ([ "foo": "bar" ]);
     }

Hope that helps,

Erwin.



More information about the DGD mailing list