Test for element in array

BCS BCS at pathlink.com
Thu Jul 13 09:51:53 PDT 2006


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


import std.stdio;

void main()
{
	int[] a = new int[10];
	int j=1;

	// populate a
	foreach(int i, inout int k; a) k=i;

	if({foreach(i;a) if(i==j) return true; return false;})
		writef("true\n");
	else
		writef("false\n");
}



More information about the Digitalmars-d-learn mailing list