Note: This website is archived. For up-to-date information about D projects and development, please visit

Associative array of strings

Part of ForeachCategory


Foreach with an associative array of dynamic strings (uses alias, too)


import std.stdio;

void main() {

    /* create an associative array called "map" */
    char[][char[]] map; 

    map["abc"] = "apple";
    map["def"] = "bee";
    map["xyz"] = "cat";

    /*  The foreach is used to determine the contents of the associative array.
        Note that the types of 'key' and 'value' are inferred.

    writefln("Iterating over map...");
    foreach (key, value; map) 
        writefln("\t%s => %s", key, value);


This example was inspired on an example posted by csauls and from comments by larsivi.