Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
July 06, 2016 AA with dynamic array value | ||||
---|---|---|---|---|
| ||||
How can I create (and update) and associative array where the key is a string, and the value is a dynamic array of integers? For example: void insertValue( int[][string]aa, string key, int value ) { int[]* keyvalue; keyvalue = ( key in aa ); if ( keyvalue !is null ) { *(keyvalue) ~ value; // This line fails. } else { int[] tmp; tmp ~= value; aa[key] = tmp; } } int main( string[] args) { int[][string] myAA; insertValue( myAA, "hello", 1 ); insertValue( myAA, "goodbye", 2 ); insertValue( myAA, "hello", 3 ); return 0; } Fails with: ...(16): Error: ~ has no effect in expression (*keyvalue ~ value) Thanks for any help. |
July 06, 2016 Re: AA with dynamic array value | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Dillabaugh | On Wednesday, 6 July 2016 at 01:58:31 UTC, Craig Dillabaugh wrote:
> *(keyvalue) ~ value; // This line fails.
That should prolly be ~= instead of ~.
|
July 06, 2016 Re: AA with dynamic array value | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Wednesday, 6 July 2016 at 02:03:54 UTC, Adam D. Ruppe wrote:
> On Wednesday, 6 July 2016 at 01:58:31 UTC, Craig Dillabaugh wrote:
>> *(keyvalue) ~ value; // This line fails.
>
> That should prolly be ~= instead of ~.
Ahh, I was so close. Thank you that seems to do the trick.
However, now I have another issue.
For the following main function:
int main( string[] args) {
int[][string] myAA;
//int[] tmp;
//tmp ~= 7;
//myAA["world"] = tmp;
insertValue( myAA, "hello", 1 );
insertValue( myAA, "goodbye", 2 );
insertValue( myAA, "hello", 3 );
foreach (k; myAA.keys.sort)
{
writefln("%3s %d", k, myAA[k].length);
}
return 0;
}
If I run this, it prints out nothing. However, if I uncomment adding an element for 'world' then it prints (as expected):
goodbye 1
hello 2
world 1
Why doesn't my function allow me to insert elements into an empty associative array, but succeeds for an AA with some element in it?
|
July 06, 2016 Re: AA with dynamic array value | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Dillabaugh | On Wednesday, 6 July 2016 at 02:19:47 UTC, Craig Dillabaugh wrote:
> Why doesn't my function allow me to insert elements into an empty associative array, but succeeds for an AA with some element in it?
this is true for any dynamic array, including AAs. until something is added to array, it actually a `null` pointer. i.e. arrays (and AAs) generally consisting of pointer to data and some length/info field. while array is empty, pointer to data is `null`. and when you passing your dynamic array slice/AA to function, it makes a copy of those internal fields. reallocing `null` doesn't affect the original variable.
generally speaking, you should use `ref` if you plan to make your dynamic array/AA grow. i.e.
void insertValue (ref int[][string]aa, string key, int value)
|
July 06, 2016 Re: AA with dynamic array value | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | On Wednesday, 6 July 2016 at 02:33:02 UTC, ketmar wrote:
> On Wednesday, 6 July 2016 at 02:19:47 UTC, Craig Dillabaugh wrote:
>> [...]
>
> this is true for any dynamic array, including AAs. until something is added to array, it actually a `null` pointer. i.e. arrays (and AAs) generally consisting of pointer to data and some length/info field. while array is empty, pointer to data is `null`. and when you passing your dynamic array slice/AA to function, it makes a copy of those internal fields. reallocing `null` doesn't affect the original variable.
>
> generally speaking, you should use `ref` if you plan to make your dynamic array/AA grow. i.e.
>
> void insertValue (ref int[][string]aa, string key, int value)
Thanks for giving me the correct solution, and for the explanation.
Cheers,
Craig
|
Copyright © 1999-2021 by the D Language Foundation