Test for element in array

BCS BCS at pathlink.com
Thu Jul 13 16:46:46 PDT 2006


Andrei Khropov wrote:
> Paolo Invernizzi wrote:
> 
> 
>>Hi all,
>>
>>Someone can suggest me the best way for testing the presence of an element in
>>an array?
>>
>>Coming from python, something like...
>>
>>if( x in array_of_x && y in array_of_y ){
>>   ...
>>}
>>
>>The workaround, apart from using a simple 'for' loop, is to use an
>>associative array, and test for key...
>>
>>---
>>Paolo
> 
> 
> Despite BCS proposed a cool solution

thanks

> I think 'in' should be built-in for 
> usual (non-associative) arrays too. why not?
> 
> 

the semantics are backwards, not a killer but its a little inconsistent.

With aa, the type of the LHS is the type of the index

if(a in b) b[a]; // valid

with non-aa, the type of the LHS would be the type of the stored value. 
The above wont always work for a non-aa. Also which a in b is found? The 
first? An array of all? The last?





More information about the Digitalmars-d-learn mailing list