DEV Community

loading...

Add to a Dictionary in D

Jesse Phillips
Senior Quality Assurance (SDET) ¶ Avid hobby D programmer ¶ Telling people what to do because I am right.
・1 min read

Dictionary, hash table, associative array, map it has many names. The main challenge with dictionary adds is more around nested associative arrays, are you going to be annoyed if I select a different name every time?

int[string] dict;
dict["Key"] = 55;
Enter fullscreen mode Exit fullscreen mode

There is a Dictionary literal in D.

auto data = ["key" : 55]
Enter fullscreen mode Exit fullscreen mode

but this is not good for adding data as it will replace the variable with a new object. However if you're nesting and the inner dictionary might be null it can be good to use for starting a dictionary.

string[int][string] data;
data["hello"] = [95: "value"];
Enter fullscreen mode Exit fullscreen mode

Discussion (0)