www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What is the preferred method for testing the existence of a key in an

reply "Gary Willoughby" <dev nomad.so> writes:
What is the preferred method for testing the existence of a key 
in an associative array?
Sep 13 2013
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Friday, 13 September 2013 at 14:03:48 UTC, Gary Willoughby 
wrote:
 What is the preferred method for testing the existence of a key 
 in an associative array?
I use the in operator: if(key in aa) { there } if(key !in aa) { not there} You can also fetch the pointer right there too: if(auto pvalue = key in aa) { auto value = *pvalue; // go ahead and use it }
Sep 13 2013
parent "Gary Willoughby" <dev nomad.so> writes:
On Friday, 13 September 2013 at 14:06:50 UTC, Adam D. Ruppe wrote:
 I use the in operator:
Aha! ta.
Sep 13 2013
prev sibling parent reply "Orvid King" <blah38621 gmail.com> writes:
On Friday, 13 September 2013 at 14:03:48 UTC, Gary Willoughby 
wrote:
 What is the preferred method for testing the existence of a key 
 in an associative array?
Well, I usually do it as: int[string] someCache; int getValue(string key) { if (auto val = key in someCache) return *val; return someCache[key] = -3; }
Sep 13 2013
parent reply Jacob Carlborg <doob me.com> writes:
On 2013-09-13 16:17, Orvid King wrote:

 Well, I usually do it as:

 int[string] someCache;

 int getValue(string key)
 {
      if (auto val = key in someCache)
          return *val;
      return someCache[key] = -3;
 }
That doesn't work with generic code. I mean, -3 can be a legal value. There are many types that doesn't have an invalid value, like pointers do. -- /Jacob Carlborg
Sep 14 2013
parent reply "simendsjo" <simendsjo gmail.com> writes:
On Saturday, 14 September 2013 at 10:50:17 UTC, Jacob Carlborg 
wrote:
 On 2013-09-13 16:17, Orvid King wrote:

 Well, I usually do it as:

 int[string] someCache;

 int getValue(string key)
 {
     if (auto val = key in someCache)
         return *val;
     return someCache[key] = -3;
 }
That doesn't work with generic code. I mean, -3 can be a legal value. There are many types that doesn't have an invalid value, like pointers do.
I don't understand. What doesn't work? If the key exists, val !is null, if it doesn't, val is null.
Sep 14 2013
parent "Namespace" <rswhite4 googlemail.com> writes:
On Saturday, 14 September 2013 at 11:32:23 UTC, simendsjo wrote:
 On Saturday, 14 September 2013 at 10:50:17 UTC, Jacob Carlborg 
 wrote:
 On 2013-09-13 16:17, Orvid King wrote:

 Well, I usually do it as:

 int[string] someCache;

 int getValue(string key)
 {
    if (auto val = key in someCache)
        return *val;
    return someCache[key] = -3;
 }
That doesn't work with generic code. I mean, -3 can be a legal value. There are many types that doesn't have an invalid value, like pointers do.
I don't understand. What doesn't work? If the key exists, val !is null, if it doesn't, val is null.
I guess, he means your default value -3 isn't appropriate for all types.
Sep 14 2013