Use .get() in MultiD Assoc Array?

Philippe Sigaud philippe.sigaud at gmail.com
Thu Aug 30 14:56:08 PDT 2012


On Thu, Aug 30, 2012 at 10:24 PM, Paul <phshaffer at gmail.com> wrote:
>
> So one array like-  aa[MyKey("abc","def","ghi")] = "my value";
>
> and another like- string[] da; da[99]="abc"~","~"def"~","~"ghi";
> or maybe-          MyKey[] da; da[99]=MyKey("abc","def","ghi");

The latter, if you group your keys, I think. You can just push the new
value at the array's end, as if it was a stack:


struct MyKey(string k1,k2,k3);
string[MyKey] aa;
MyKey[] da;

// pushing "value" with key ("abc","def","ghi"):

MyKey k = MyKey("abc","def","ghi");
aa[k] = "value";
da ~= k; // append k to da

Jonathan's advice of recording the key's index (in the array) with the
value is interesting, but then you need a special value in the array
to indicate a discarded key. Here the recent thread about Option!T
comes to mind... but I don't know what you're trying the achieve, so
let's not go into complicated stuff right now.


> This thread has been quite helpful already.  Thanks to all

You're welcome. Note that your need of having a structure which is
both associative and ordered is, if not unheard-of, at least somewhat
uncommon.


More information about the Digitalmars-d-learn mailing list