Jump to page: 1 2 3
Thread overview
Test for element in array
Jul 13, 2006
Paolo Invernizzi
Jul 13, 2006
BCS
Jul 13, 2006
Dave
Jul 13, 2006
BCS
Jul 13, 2006
Kirk McDonald
Jul 13, 2006
Andrei Khropov
Jul 14, 2006
Don Clugston
Jul 14, 2006
BCS
Jul 14, 2006
Lars Ivar Igesund
Jul 14, 2006
Dave
Jul 13, 2006
Andrei Khropov
Jul 13, 2006
BCS
Jul 14, 2006
Regan Heath
Jul 14, 2006
Paolo Invernizzi
Jul 14, 2006
Ivan Senji
Jul 14, 2006
Andrei Khropov
Jul 14, 2006
BCS
Jul 14, 2006
Carlos Santander
Jul 15, 2006
Derek Parnell
Jul 15, 2006
Kirk McDonald
Jul 15, 2006
Paolo Invernizzi
July 13, 2006
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
July 13, 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");
}
July 13, 2006
BCS 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
> 
> 
> 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");
> }

That's a very elegant solution and it should work, but according to my tests it doesn't (darn). It never returns false.

Set j = 100 once and give it a try.
July 13, 2006
Dave wrote:
> BCS 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
>>
>>
>>
>> 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");
>> }
> 
> 
> That's a very elegant solution and it should work, but according to my tests it doesn't (darn). It never returns false.
> 
> Set j = 100 once and give it a try.

Bizarre

--     if({foreach(i;a) if(i==j) return true; return false;})
++     if({foreach(i;a) if(i==j) return true; return false;}())

this fixes it.

WT...

Oh.

{return false;}.typeof == bool delegate()
{return false;}().typeof == bool


July 13, 2006
BCS wrote:
> Dave wrote:
> 
>> BCS 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
>>>
>>>
>>>
>>>
>>> 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");
>>> }
>>
>>
>>
>> That's a very elegant solution and it should work, but according to my tests it doesn't (darn). It never returns false.
>>
>> Set j = 100 once and give it a try.
> 
> 
> Bizarre
> 
> --     if({foreach(i;a) if(i==j) return true; return false;})
> ++     if({foreach(i;a) if(i==j) return true; return false;}())
> 
> this fixes it.
> 
> WT...
> 
> Oh.
> 
> {return false;}.typeof == bool delegate()
> {return false;}().typeof == bool
> 
> 

Hmmm. "Lambda properties." Fun.

-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://dsource.org/projects/pyd/wiki
July 13, 2006
BCS wrote:

>  if({foreach(i;a) if(i==j) return true; return false;}())

Yeah, this new delegate syntax is cool !

-- 

July 13, 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

Despite BCS proposed a cool solution I think 'in' should be built-in for usual (non-associative) arrays too. why not?


-- 

July 13, 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?


July 14, 2006
BCS wrote:
> Dave wrote:
>> BCS 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
>>>
>>>
>>>
>>> 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");
>>> }
>>
>>
>> That's a very elegant solution and it should work, but according to my tests it doesn't (darn). It never returns false.
>>
>> Set j = 100 once and give it a try.
> 
> Bizarre
> 
> --     if({foreach(i;a) if(i==j) return true; return false;})
> ++     if({foreach(i;a) if(i==j) return true; return false;}())
> 
> this fixes it.
> 
> WT...
> 
> Oh.
> 
> {return false;}.typeof == bool delegate()
> {return false;}().typeof == bool
> 
> 

Ah... Thanks.
July 14, 2006
On Thu, 13 Jul 2006 16:46:46 -0700, BCS <BCS@pathlink.com> wrote:
> 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.

True.. (wild idea alert) unless we make it valid to index an array with a value, which in turn returns the index of that value. It would perform a linear search from start to end (or match) therefore returning the 'first' match.

> The above wont always work for a non-aa. Also which a in b is found? The first? An array of all? The last?

In the case of 'in' aren't you just asking "is it in there", in which case it could return the first, or last, doesn't really matter.

However, 'in' does return a pointer to the value .. I think returning the first is the least surprising, most useful thing to do. It would also be consistent with the index by value idea above.

So, if you want the last match you say:
  if (a in b.reverse)

and if you want the 2nd, 3rd, 4th etc
  p = (a in b);           //gets first
  p = (a in b[b[*p]..$]); //2nd
  p = (a in b[b[*p]..$]); //3rd
  p = (a in b[b[*p]..$]); //4th

funky! ;)

Regan
« First   ‹ Prev
1 2 3