|
| 1 | +Ddoc |
| 2 | + |
| 3 | +$(COZUM_BOLUMU Associative Arrays) |
| 4 | + |
| 5 | +$(OL |
| 6 | + |
| 7 | +$(LI |
| 8 | + |
| 9 | +$(UL |
| 10 | + |
| 11 | +$(LI |
| 12 | +The $(C .keys) property returns a slice (i.e. dynamic array) that includes all of the keys of the associative array. Iterating over this slice and removing the element for each key by calling $(C .remove) would result in an empty associative array: |
| 13 | + |
| 14 | +--- |
| 15 | +import std.stdio; |
| 16 | + |
| 17 | +void main() { |
| 18 | + string[int] names = |
| 19 | + [ |
| 20 | + 1 : "one", |
| 21 | + 10 : "ten", |
| 22 | + 100 : "hundred", |
| 23 | + ]; |
| 24 | + |
| 25 | + writeln("Initial length: ", names.length); |
| 26 | + |
| 27 | + int[] keys = names.keys; |
| 28 | + |
| 29 | + /* 'foreach' is similar but superior to 'for'. We will |
| 30 | + * see the 'foreach' loop in the next chapter. */ |
| 31 | + foreach (key; keys) { |
| 32 | + writefln("Removing the element %s", key); |
| 33 | + names.remove(key); |
| 34 | + } |
| 35 | + |
| 36 | + writeln("Final length: ", names.length); |
| 37 | +} |
| 38 | +--- |
| 39 | + |
| 40 | +$(P |
| 41 | +That solution may be slow especially for large arrays. The following methods would empty the array in a single step. |
| 42 | +) |
| 43 | + |
| 44 | +) |
| 45 | + |
| 46 | +$(LI |
| 47 | +Another solution is to assign an empty array: |
| 48 | + |
| 49 | +--- |
| 50 | + string[int] emptyAA; |
| 51 | + names = emptyAA; |
| 52 | +--- |
| 53 | + |
| 54 | +) |
| 55 | + |
| 56 | +$(LI |
| 57 | +Since the initial value of an array is an empty array anyway, the following technique would achieve the same result: |
| 58 | + |
| 59 | +--- |
| 60 | + names = names.init; |
| 61 | +--- |
| 62 | + |
| 63 | +) |
| 64 | + |
| 65 | +) |
| 66 | + |
| 67 | +) |
| 68 | + |
| 69 | +$(LI |
| 70 | +The goal is to store multiple grades per student. Since multiple grades can be stored in a dynamic array, an associative array that maps from $(C string) to $(C int[]) would work here. The grades can be appended to the dynamic arrays that are stored in the associative array: |
| 71 | + |
| 72 | +--- |
| 73 | +import std.stdio; |
| 74 | + |
| 75 | +void main() { |
| 76 | + /* The key type of this associative array is string and |
| 77 | + * the value type is int[], i.e. an array of ints. The |
| 78 | + * associative array is being defined with an extra |
| 79 | + * space in between to help distinguish the value type: */ |
| 80 | + int[] [string] grades; |
| 81 | + |
| 82 | + /* The array of ints that correspond to "emre" is being |
| 83 | + * used for appending the new grade to that array: */ |
| 84 | + grades["emre"] ~= 90; |
| 85 | + grades["emre"] ~= 85; |
| 86 | + |
| 87 | + /* Printing the grades of "emre": */ |
| 88 | + writeln(grades["emre"]); |
| 89 | +} |
| 90 | +--- |
| 91 | + |
| 92 | +$(P |
| 93 | +The grades can also be assigned in one go with an array literal: |
| 94 | +) |
| 95 | + |
| 96 | +--- |
| 97 | +import std.stdio; |
| 98 | + |
| 99 | +void main() { |
| 100 | + int[][string] grades; |
| 101 | + |
| 102 | + grades["emre"] = [ 90, 85, 95 ]; |
| 103 | + |
| 104 | + writeln(grades["emre"]); |
| 105 | +} |
| 106 | +--- |
| 107 | + |
| 108 | +) |
| 109 | + |
| 110 | +) |
| 111 | + |
| 112 | +Macros: |
| 113 | + SUBTITLE=Associative Arrays Solutions |
| 114 | + |
| 115 | + DESCRIPTION=Programming in D exercise solutions: Associative Arrays |
| 116 | + |
| 117 | + KEYWORDS=programming in d tutorial associative arrays |
0 commit comments