If the associative array contains a key in D

The question is pretty clear; I want to be able to check if the associative array contains the value that I am going to (potentially) insert before inserting it. Is there an easy way to do this without searching through dict.keys ? Maybe something like if (dict.contains(val)) ... ?

+6
source share
2 answers

To check if a key is in an associative array, use the in operator:

 string[int] aa; string* ps = 100 in aa; if(ps) { // 100 is a key in aa, ps is a pointer to the corresponding value } else { // 100 is not a key in aa } 

To check if a value exists, you need to search through aa.values .

+7
source
 if (!dict.get(key, null)) dict[key] = val; 
+1
source

All Articles