Use .get() in MultiD Assoc Array?

Paul phshaffer at gmail.com
Thu Aug 30 11:29:23 PDT 2012


On Thursday, 30 August 2012 at 18:20:02 UTC, Philippe Sigaud 
wrote:
> On Thu, Aug 30, 2012 at 7:18 PM, Paul <phshaffer at gmail.com> 
> wrote:
>> From the book a way to respond to a non-existent key in an 
>> assoc. array:
>>
>> assert(aa["hello"] == "ciao");
>> // Key "hello" exists, therefore ignore the second argume
>> assert(aa.get("hello", "salute") == "ciao");
>> // Key "yo" doesn’t exist, return the second argument
>> assert(aa.get("yo", "buongiorno") == "buongiorno");
>>
>> Should this work in multidimensional arrays?
>>
>> aa.get("key1" "key2" "key2", "nonexistent") == "sometext"
>
> D multi-key associative arrays do not exist as such, they are
> associative arrays inside one another. When you write
>
> int[string][string][double] aa;
>
> you can use get() on aa, but only on its keys, which are of type
> double, whereas its values are of type int[string][string].
>
> I guess an effect similar to what you're asking can be obtained 
> by
> using a tuple as a key:
>
> import std.typecons;
> int[Tuple!(string,string,double)] aa;
>
> auto p = aa.get(tuple("abc","def", 3.14), 0);  // 0 is the 
> default value.

my array is of the form string[string][string][string] abc;


More information about the Digitalmars-d-learn mailing list