On Thursday, 30 August 2012 at 18:29:28 UTC, Paul wrote:
On Thursday, 30 August 2012 at 18:20:02 UTC, Philippe Sigaud wrote:
On Thu, Aug 30, 2012 at 7:18 PM, Paul <phshaf...@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;

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. 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.

Reply via email to