From bcb11b736b1c3665c751bd1b15c4d89ae0914cd3 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Fri, 13 Apr 2018 18:08:02 +0200 Subject: [PATCH] add section about associative arrays --- hands-on_dlang.md | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/hands-on_dlang.md b/hands-on_dlang.md index 7600220..9bd36f3 100644 --- a/hands-on_dlang.md +++ b/hands-on_dlang.md @@ -56,8 +56,8 @@ brew cask install visual-studio-code * Open the Extension view in the sidebar: |Operating system|Shortcut | |----------------|---------| - |OS X |⌘ + ⇧ + X| - |Windows |⌃ + ⇧ + X| + |OS X|⌘ + ⇧ + X| + |Windows|⌃ + ⇧ + X| * Install the extension “D Programming Language (code-d)” (requires that git is installed). * Restart Visual Studio Code. @@ -721,3 +721,17 @@ Slices are the most prominent example of `RandomAccessRange`s in The D standard library provides a huge arsenal of lazy range algorithm functions. Most of them can be found in in the `std.range` and `std.algorithm` packages. + +### Associative arrays + +D has builtin hashmaps, which are called _associative arrays_: + +```D +int[string] map; // keys of type string, values of type int +map["key1"] = 10; // insertion or modification, if the key already exists +if ("key1" in map) { // checking if a key is in an associative array + writeln("key1 is in map"); +} +assert(map.length == 1); // associative arrays provide a .length property +map.remove("key1"); // remove a key from an associative array +```