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

Ali Çehreli acehreli at yahoo.com
Sat Jul 1 05:26:07 UTC 2023


On 6/30/23 17:42, Cecil Ward wrote:

 > https://dlang.org/spec/hash-map.html#testing_membership in the language
 > docs, under associative arrays - 13.3 testing membership. Would anyone
 > else care to try that example out as that might be quicker?

I tried it by

1) Putting all the code inside a 'void main()' function
2) Pasting the code from the top of that page

and it works:

void main() {
     int[string] aa;   // Associative array of ints that are
     // indexed by string keys.
     // The KeyType is string.
     aa["hello"] = 3;  // set value associated with key "hello" to 3
     int value = aa["hello"];  // lookup value from a key
     assert(value == 3);
     int* p;

     p = "hello" in aa;
     if (p !is null)
     {
         *p = 4;  // update value associated with key
         assert(aa["hello"] == 4);
     }
}

 > the only substantive
 > change being deleting the variable p

Do you mean this:

     aa.remove("hello");

That works too.

D's associative arrays have a few quirks but they work just fine. They 
are not buggy as it may be inferred from some of the posts here.

Ali



More information about the Digitalmars-d-learn mailing list