Comparing Multiple Values

Alexander Panek alexander.panek at brainsware.org
Tue Mar 11 09:55:02 PDT 2008


okibi wrote:
> I've looked all over the site but can't seem to find if D supports any kind of like IN statement for comparisons. For example, I can do the following in SQL:
> 
> select *
> from table
> where value in (1,2,3)
> 
> And it will compare it to 1, 2, and 3. Is this possible to do within D's if statement? I hate to go about it as such:
> 
> if (value == 1 || value == 2 || value == 3)
>     dosomething();
> 
> Just seems like this could be written better. Can anyone give me any pointers?
> 
> Thanks!

bool includes (T) (T[] array, item) {
   foreach (i; array) {
     if ( item == i) { return true; }
   }

   return false;
}

if (["a", "b", "c"].includes("a")) {
   doSomething();
}

untested, and almost the same as Frank's, just a tad more Ruby-ish. ;)


More information about the Digitalmars-d-learn mailing list