comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Succinct way of testing array membership
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Succinct way of testing array membership [message #81201] Thu, 30 August 2012 12:16 Go to next message
wlandsman is currently offline  wlandsman
Messages: 743
Registered: June 2000
Senior Member
For very large vectors, WHERE() methods are slow because (1) you compute the index of where the member is found, even if you just care about membership, and more important (2) WHERE operates on the entire vector, and does not exit as soon as the member is found. A faster method would be

function array_contains,v,x
;Is x an element of V?
return,~array_equal( (v eq x), 0b)
end

since ARRAY_EQUAL exits as soon as the first match is found. I can't imagine the speed difference is significant for normal size vectors, though.

--Wayne



On Thursday, August 30, 2012 12:51:48 PM UTC-4, godber wrote:
> Is there a more succinct way of testing array membership than using where and n_elements on the indexes? Something like array_contains(a, 'pancakes')?
>
>
>
> Austin
Re: Succinct way of testing array membership [message #81203 is a reply to message #81201] Thu, 30 August 2012 10:09 Go to previous messageGo to next message
Michael Galloy is currently offline  Michael Galloy
Messages: 1114
Registered: April 2006
Senior Member
On 8/30/12 10:51 AM, godber wrote:
> Is there a more succinct way of testing array membership than using where and n_elements on the indexes? Something like array_contains(a, 'pancakes')?
>
> Austin
>

In general, how about:

print, where(a eq 'pancakes', /null) ? 'found' : 'not found'

Mike
--
Michael Galloy
www.michaelgalloy.com
Modern IDL: A Guide to IDL Programming (http://modernidl.idldev.com)
Research Mathematician
Tech-X Corporation
Re: Succinct way of testing array membership [message #81204 is a reply to message #81203] Thu, 30 August 2012 09:59 Go to previous messageGo to next message
Russell Ryan is currently offline  Russell Ryan
Messages: 122
Registered: May 2012
Senior Member
On Thursday, August 30, 2012 12:51:48 PM UTC-4, godber wrote:
> Is there a more succinct way of testing array membership than using where and n_elements on the indexes? Something like array_contains(a, 'pancakes')?
>
>
>
> Austin

wouldn't that just be liek this:

suppose:
t=['pancakes','eggs','milk','sausage'] ;array to test

then:
g=where(strmatch(t,'pancakes',/fold),n) ;the line testing it
if n ne 0 then print,'Its in the array!'


-russell
Re: Succinct way of testing array membership [message #81205 is a reply to message #81204] Thu, 30 August 2012 09:57 Go to previous messageGo to next message
DavidF[1] is currently offline  DavidF[1]
Messages: 94
Registered: April 2012
Member
Godber writes:

> Is there a more succinct way of testing array membership than using where and n_elements on the indexes? Something like array_contains(a, 'pancakes')?

It might take you 30 seconds to write such a thing!

Cheers,

David
Re: Succinct way of testing array membership [message #81298 is a reply to message #81203] Thu, 30 August 2012 14:12 Go to previous message
Helder Marchetto is currently offline  Helder Marchetto
Messages: 520
Registered: November 2011
Senior Member
On Thursday, August 30, 2012 7:09:35 PM UTC+2, Mike Galloy wrote:
> On 8/30/12 10:51 AM, godber wrote:
>
>> Is there a more succinct way of testing array membership than using where and n_elements on the indexes? Something like array_contains(a, 'pancakes')?
>
>>
>
>> Austin
>
>>
>
>
>
> In general, how about:
>
>
>
> print, where(a eq 'pancakes', /null) ? 'found' : 'not found'
>
>
>
> Mike
>
> --
>
> Michael Galloy
>
> www.michaelgalloy.com
>
> Modern IDL: A Guide to IDL Programming (http://modernidl.idldev.com)
>
> Research Mathematician
>
> Tech-X Corporation

Hi Mike,
I never noticed the presence of the /null keyword. Nice tip.
However, the version you provided will not work (at least not on my pc...).
How about:
print, (where(a EQ 'pancakes', /null) NE !NULL) ? 'found' : 'not found'
or
print, ((where(a EQ 'pancakes'))[0] GE 0) ? 'found' : 'not found'

Not as clean, but still doing the job.

Cheers,
Helder
Re: Succinct way of testing array membership [message #81299 is a reply to message #81205] Thu, 30 August 2012 13:57 Go to previous message
godber is currently offline  godber
Messages: 4
Registered: January 2012
Junior Member
On Thursday, August 30, 2012 9:57:02 AM UTC-7, Coyote wrote:
> Godber writes:
>
>
>
>> Is there a more succinct way of testing array membership than using where and n_elements on the indexes? Something like array_contains(a, 'pancakes')?
>
>
>
> It might take you 30 seconds to write such a thing!
>
>
>
> Cheers,
>
>
>
> David

Yeah David it does, but look at all the nice trys by people below. In fact some of them were very informative. I like Mike's suggestion, and it points out /null to me and wayne is kind enough to point out a quicker solution. Therefor I declare the question a success.

I mean, it still is a little surprising to me that there isn't an inbuilt way to test list or array membership.

Thanks for the alternatives guys.

Austin
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: A simple DLM question
Next Topic: plotting symbols on map using direct graphics 8.2

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 15:47:13 PDT 2025

Total time taken to generate the page: 0.00542 seconds