Use .get() in MultiD Assoc Array?

Philippe Sigaud philippe.sigaud at gmail.com
Thu Aug 30 12:33:23 PDT 2012


On Thu, Aug 30, 2012 at 8:57 PM, Paul <phshaffer at gmail.com> wrote:

> Maybe I'm not going about my project from the best angle?  Another problem I
> have is when I go to printout my array, being associative, it is not in the
> order I built it.  It would help greatly if I could print it in order.

Associative arrays reorganize themselves to allow for fast
insertion/retrieval. They do not make any promise concerning order or
printing.

> Maybe I should look into this "tuple" thing more.  As well I would like to
> be able to check for the existence of a particular "key" quickly without
> setting up one those three-tier foreach iteration loops.

A tuple is just a grouping of values together, its just a handy
predefined struct-maker in std.typecons. In your case, its equivalent
to defining

struct MyKey { string a,b,c}

string[MyKey] aa;

to fill it:  aa[MyKey("abc","def","ghi")] = "my value";

It depends whether you need intermediate associative arrays
(aa["abc"], aa["abc"]["def"]) or not. It's like a 2D/3D array: do you
want access to lines / arrays or just to individual elements. In your
case, if you just want access to individual elements, using a key
grouping all your key strings under one struct might be interesting:
it means just one query in the AA.

To check for a key:
auto wanted = MyKey("abc", "def", "ghi");

if (auto p = wanted in aa)   // p !is null, => the key is in the
associative array
{
    // use *p
}


More information about the Digitalmars-d-learn mailing list