Delete All Keys From Associative Array
Part of ArraysCategory
Description
Show a couple ways that all keys can be removed from an associate array.
Example
/* inspired from: http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=4199 */ void main() { char[][char[]] aA; printf("Fill in the array.\n"); aA["a"] = "Apple"; aA["b"] = "Book"; aA["c"] = "Car"; checkArray(aA); /* Clearing method from http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=4201 */ printf("Clear out the items (implementation-dependent).\n"); aA = null; checkArray(aA); printf("\nFill in the array, again.\n"); aA["a"] = "Apple"; aA["b"] = "Book"; aA["c"] = "Car"; checkArray(aA); /* Clearing method from http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=4202 */ /* Updated to reflect addition of 'remove' method */ printf("Removes it from the hash.\n"); foreach(char[] key; aA.keys) aA.remove(key); checkArray(aA); } void checkArray(char[][char[]] assocArr) { printf("%d\t", assocArr.length); if("a" in assocArr) printf("yes 'a'\t"); else printf("no 'a'\t"); if("b" in assocArr) printf("yes 'b'\t"); else printf("no 'b'\t"); if("c" in assocArr) printf("yes 'c'\t"); else printf("no 'c'\t"); if("d" in assocArr) printf("yes 'd'\n"); else printf("no 'd'\n"); }
