Bug in usage of associative array: dynamic array with string as a key

Cecil Ward cecil at cecilward.com
Sat Jul 1 00:55:05 UTC 2023


On Friday, 30 June 2023 at 19:05:23 UTC, Cecil Ward wrote:
> I have code roughly like the following:
>
>    dstring str = "name"d;
>    uint ordinal =  (( str in Decls.ordinals ) !is null)  ?  
> Decls.ordinals[ str ]  :  -1;
>
> struct Decls
>    {
>    uint[ dstring]   ordinals;
>    }
>
> //and
>    Decls.ordinals[ str ] = ordinal_counter++;
>
> The problem is that it always returns ordinal== -1 from the 
> expression. Can you sort me out? I took this from the example 
> given in the language reference under arrays, testing for 
> membership (or similar, I forget the subssection title).
>
> From good old printfs it seems to be the case that the array is 
> being populated (elsewhere) with the expected correct values. 
> Taking out the if doesn’t seem to help either. I don’t have a 
> way of examining the contents of the dynamic array directly to 
> check that they are actually being stored as expected, other 
> than seeing that that line of code is indeed being executed 
> with the expected values of str going in. Note that I’m using 
> 32-bit dstrings everywhere, not strings of bytes.

Fool that I am. I did those good old printfs a while back, and 
now I recheck them I see that something has become broken and it 
seems that the insertions are not happening now. So thankyou for 
your kindness and I’ll post again if that doesn’t solve the 
non-issue.


More information about the Digitalmars-d-learn mailing list