Add to a Dictionary in D

jessekphillips

Jesse Phillips

Posted on September 10, 2019

Add to a Dictionary in D

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
💖 💪 🙅 🚩
jessekphillips
Jesse Phillips

Posted on September 10, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Memory Free Programming in D
dlang Memory Free Programming in D

February 11, 2020

Regular Expressions in D
regex Regular Expressions in D

December 30, 2019

Printing in D
dlang Printing in D

December 15, 2019

List Comprehension in D
dlang List Comprehension in D

December 13, 2019

Compile-Time Reflection
dlang Compile-Time Reflection

December 9, 2019