www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Static Initialization of Associative Arrays

reply Tyro[a.c.edwards] <no spam.com> writes:
D v2.012 currently allows the following:

int[char[]] eroMatrix = ["SA":1, "SB":2, "SP":3, "KK":4];
foreach(k, v; eroMatrix)
  writefln(k, "->", v);

// output:
SA->1
SB->2
SP->3
KK->4

However attempts to directly access a "key:value" pair results in an
ArrayBoundsError. Any idea why? Is there a temporary workaround available?

writefln(eroMatrix["SA"]) // ArrayBoundsError

Thanks,
Andrew
Mar 30 2008
next sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Mon, 31 Mar 2008 07:52:17 +0200, Tyro[a.c.edwards] <no spam.com> wrot=
e:

 D v2.012 currently allows the following:

 int[char[]] eroMatrix =3D ["SA":1, "SB":2, "SP":3, "KK":4];
 foreach(k, v; eroMatrix)
   writefln(k, "->", v);

 // output:
 SA->1
 SB->2
 SP->3
 KK->4

 However attempts to directly access a "key:value" pair results in an  =

 ArrayBoundsError. Any idea why? Is there a temporary workaround  =

 available?

 writefln(eroMatrix["SA"]) // ArrayBoundsError

 Thanks,
 Andrew

Weird. This works under DMD 1.026, and under DMD 2.012 if I don't use th= e = literal.
Mar 31 2008
prev sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Tyro[a.c.edwards]" <no spam.com> wrote in message 
news:fspu6h$2kps$1 digitalmars.com...
D v2.012 currently allows the following:

 int[char[]] eroMatrix = ["SA":1, "SB":2, "SP":3, "KK":4];
 foreach(k, v; eroMatrix)
  writefln(k, "->", v);

 // output:
 SA->1
 SB->2
 SP->3
 KK->4

 However attempts to directly access a "key:value" pair results in an 
 ArrayBoundsError. Any idea why? Is there a temporary workaround available?

 writefln(eroMatrix["SA"]) // ArrayBoundsError

 Thanks,
 Andrew

I would have expected something like: writefln(eroMatrix[eroMatrix.keys[0]]); to work, but it doesn't. O_o
Mar 31 2008
parent badmadevil <badmadevil gmail.com> writes:
Jarrett Billingsley wrote:
 "Tyro[a.c.edwards]" <no spam.com> wrote in message 
 news:fspu6h$2kps$1 digitalmars.com...
 D v2.012 currently allows the following:

 int[char[]] eroMatrix = ["SA":1, "SB":2, "SP":3, "KK":4];
 foreach(k, v; eroMatrix)
  writefln(k, "->", v);

 // output:
 SA->1
 SB->2
 SP->3
 KK->4

 However attempts to directly access a "key:value" pair results in an 
 ArrayBoundsError. Any idea why? Is there a temporary workaround available?

 writefln(eroMatrix["SA"]) // ArrayBoundsError

 Thanks,
 Andrew

I would have expected something like: writefln(eroMatrix[eroMatrix.keys[0]]); to work, but it doesn't. O_o

It seems matter only the _method_ of initialization. int[char[]] eroMatrix = ["SA":1, "SB":2, "SP":3, "KK":4]; // literal eroMatrix["00"] = 9 ; // direct foreach(k; eroMatrix.keys.sort) { writef(k, "->", typeid(typeof(k)), "->") ; writefln( eroMatrix[k]); }
 OUTPUT:



KK->const(char)[]-> Error: ArrayBoundsError aatst(10) PS:D 2.0 change the key's type from char[] to const(char)[], D 1.0 not.
 

Mar 31 2008