D has built-inassociative arrays also known as hash maps.An associative array with a key type ofstring and a value typeofint is declared as follows:
int[string] arr;The value can be accessed by its key and thus be set:
arr["key1"] = 10;To test whether a key is located in the associative array, thein expression can be used:
if ("key1" in arr) writeln("Yes");Thein expression returns a pointer to the value if itcan be found or anull pointer otherwise. Thus existence checkand writes can be conveniently combined:
if (auto val = "key1" in arr) *val = 20;Access to a key which doesn't exist yields aRangeErrorthat immediately aborts the application. For safe accesswith a default value,get(key, defaultValue) can be used.
AA's have the.length property like arrays and providea.remove(key) member to remove entries by their key.It is left as an exercise to the reader to explorethe special.byKey and.byValue ranges.