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

Home » Public Forums » archive » 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
Succinct way of testing array membership [message #81207] Thu, 30 August 2012 09:51 Go to next message
godber is currently offline  godber
Messages: 4
Registered: January 2012
Junior Member
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 #81289 is a reply to message #81207] Fri, 31 August 2012 10:26 Go to previous message
Michael Galloy is currently offline  Michael Galloy
Messages: 1114
Registered: April 2006
Senior Member
On 8/30/12 3:12 PM, Helder wrote:
> 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
>

I like Wayne's approach using ARRAY_EQUAL. I was trying to get too cute
and eliminating the comparison to !null, try:

IDL> a = ['cakes', 'pies']
IDL> print, where(a eq 'cakes', /null) ne !null ? 'found' : 'not found'
found
IDL> print, where(a eq 'pancakes', /null) ne !null ? 'found' : 'not 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 #81294 is a reply to message #81207] Fri, 31 August 2012 07:48 Go to previous 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

Yeah, the /null feature is nice. But be careful, because /null was only added to IDL in v8.0. So, if you expect your code to be distributed to someone with pre 8.0 (and I know lots of people still running 7), then this will fail.


-russell
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: get lat/lon from image with MAP_SET
Next Topic: Geodetic functions library GeoDLL 32 Bit and 64 Bit

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

Current Time: Wed Oct 08 13:39:55 PDT 2025

Total time taken to generate the page: 0.00546 seconds