www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to use an associative array with array values.

reply tipdbmp <email example.com> writes:
  // foo is an associative array/hashtable with
  // key type: string
  // value type: int[10]
  //
int[10][string] foo;

// foo["a"] = new int[10]; // Range violation at runtime

// foo["a"][0] = 1; // Range violation at runtime
Mar 21 2018
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 21 March 2018 at 15:53:32 UTC, tipdbmp wrote:
 int[10][string] foo;
One option is to initialize like this --- void main() { int[10][string] foo; if("a" !in foo) foo["a"] = [0,0,0,0,0,0,0,0,0,0]; // set all to zero to create the key foo["a"][4] = 4; // now valid to set individual element } ---
Mar 21 2018
parent reply tipdbmp <email example.com> writes:
I see. I guess the other would be:

{
     int[8192] bar;
     int[8192][string] foo;
     foo["a"] = bar;
     foo["a"][8191] = -1;
}
Mar 21 2018
parent Jesse Phillips <Jesse.K.Phillips+D gmail.com> writes:
On Wednesday, 21 March 2018 at 18:31:29 UTC, tipdbmp wrote:
 I see. I guess the other would be:

 {
     int[8192] bar;
     int[8192][string] foo;
     foo["a"] = bar;
     foo["a"][8191] = -1;
 }
https://run.dlang.io/is/AK2X2t Are you looking to use static arrays or dynamic? You can't use the new keyword if it static.
Mar 21 2018