Jump to content

map: check existing value with key


photo

Recommended Posts

I use map for save int values with string keys:

 

int listTable[];

idTextTable["key"] = 10;

 

When I want check if value is exists by key "key":

 

if (listTable["key"] != NULL)
 log.message("listTable[id]: " + listTable["key"]);

 

This is work only if "key" and value 10 added earlier. If I use that:

int listTable[];

if (listTable["key"] != NULL)
 log.message("listTable[id]: " + listTable["key"]);

 

this is cause to error. How I could check if value is exists by key? Thanks

Link to comment

int listTable[];

if (listTable.check("key") == 1)
 log.message("listTable[id]: " + listTable["key"]);

The check() method checks for the existence of a key or index.

 

 

int listTable[];
idTextTable["key"] = NULL; // 0
if (listTable["key"] == NULL)
 log.message("key maps to NULL(0)");

The [] access operator accesses using key or index, without checking for existence.

Checking to see if the accessed location is NULL checks to see if the "key" is mapped to a value of NULL, not whether it exists or not.

 

Hope that helps! :)

Link to comment
×
×
  • Create New...