Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

test if an associative array contains a key in D

The question is pretty self-explanatory; I want to be able to check if an associative array contains a value I'm about to (potentially) insert, before I actually insert it. Is there a simple way to do this without searching through dict.keys? Maybe something like if (dict.contains(val)) ...?

like image 714
limp_chimp Avatar asked Feb 14 '13 23:02

limp_chimp


2 Answers

To test 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 test if a value exists, you'll have to search through aa.values.

like image 61
beerboy Avatar answered Sep 27 '22 02:09

beerboy


if (!dict.get(key, null))
     dict[key] = val;
like image 38
fwend Avatar answered Sep 23 '22 02:09

fwend