Check/Compare a value using "in"
Adam D. Ruppe
destructionator at gmail.com
Fri Nov 2 18:51:39 PDT 2012
On Saturday, 3 November 2012 at 01:42:21 UTC, MattCoder wrote:
> I need to check if some value exists in a list (Array).
Try something like this:
import std.algorithm;
if(['+', '-'].canFind(ch)) {
// it was there
}
You could also write a little function to reverse the order
bool inside(T)(T t, T[] arr) {
return arr.canFind(t);
}
void main() {
assert('+'.inside(['+', '-']));
}
The in operator doesn't work on built in arrays because the
language designers worry about matching the speed and semantics
you expect from in when used on associative arrays.
But the functions are easy to use too so IMO in array isn't
needed anyway.
More information about the Digitalmars-d-learn
mailing list