Thread overview | ||||||
---|---|---|---|---|---|---|
|
March 21, 2018 How to use an associative array with array values. | ||||
---|---|---|---|---|
| ||||
// 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 |
March 21, 2018 Re: How to use an associative array with array values. | ||||
---|---|---|---|---|
| ||||
Posted in reply to tipdbmp | 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
}
---
|
March 21, 2018 Re: How to use an associative array with array values. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | I see. I guess the other would be: { int[8192] bar; int[8192][string] foo; foo["a"] = bar; foo["a"][8191] = -1; } |
March 21, 2018 Re: How to use an associative array with array values. | ||||
---|---|---|---|---|
| ||||
Posted in reply to tipdbmp | 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. |
Copyright © 1999-2021 by the D Language Foundation