Comparing Multiple Values
Bill Baxter
dnewsgroup at billbaxter.com
Tue Mar 11 05:36:01 PDT 2008
okibi wrote:
> Frank Benoit Wrote:
>
>> okibi schrieb:
>>> 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!
>> *untested*
>>
>> bool isOneOf(T)( T value, T[] compares ... ){
>> foreach( c; compares ){
>> if( value == c ) return true;
>> }
>> return false;
>> }
>> if ( isOneOf( value, 1, 2, 3 ))
>> dosomething();
>
> This is what I was looking for, thanks!
>
> Is there any chance of the in [] method getting into D or is it a definite no?
Probably not much chance, since Walter is one of the ones who thinks A
in [x,y,z] should return true if A is 0 1 or 2, regardless of the values
of x,y, and z.
See big long argument here:
http://d.puremagic.com/issues/show_bug.cgi?id=1323
But Walter has been known to change his mind on occasion.
--bb
More information about the Digitalmars-d-learn
mailing list