Re: How do I test for existence of a list element?
- To: mathgroup at smc.vnet.net
 - Subject: [mg112766] Re: How do I test for existence of a list element?
 - From: Joseph Gwinn <joegwinn at comcast.net>
 - Date: Wed, 29 Sep 2010 04:17:05 -0400 (EDT)
 - References: <i7sens$ppn$1@smc.vnet.net>
 
In article <i7sens$ppn$1 at smc.vnet.net>,
 Bill Rowe <readnews at sbcglobal.net> wrote:
> On 9/27/10 at 5:48 AM, andre.robin3 at wanadoo.fr (andre.robin3) wrote:
> 
> >I recently had the same problem, but for a more general case : How
> >to test the existence of a deeply embedded element in a ragged
> >structure ?
> 
> >Say that the position of the element is specified by : list={i,j,k
> >..}
> 
> >my solution was something like :
> >existenceTestQ[exp_, list_] := Module[{res},
> >res = False;
> >ReplacePart[exp, list :> (res = True)];
> >res
> >]
> 
> >may be not elegant, but sufficient
> 
> =46irst generate a small ragged array
> 
> In[5]:= ragged =
>   MapThread[
>    Take[#1, #2] &, {RandomInteger[{0, 100}, {5, 10}],
>     RandomInteger[{1, 10}, 5]}]
> 
> Out[5]= {{58,15,40,35,61,97,62,21,64,23},{44,68},{39,49},{43,62,99,10,51,=
> 4},{1,6,83,6,11,93,89,67}}
> 
> And now test for something known to be present:
> 
> In[6]:= Length /@ {Position[ragged, 1, Infinity],
>    Cases[ragged, {___, 1, ___}], Cases[ragged, 1, Infinity]}
> 
> Out[6]= {1,1,1}
> 
> And finally test for something known not to be present
> 
> In[7]:= Length /@ {Position[ragged, 0, Infinity],
>    Cases[ragged, {___, 0, ___}], Cases[ragged, 0, Infinity]}
> 
> Out[7]= {0,0,0}
> 
> That is either Cases or Position can be used to determine the
> presence/absence of something in a ragged array.
Here is one general approach then, based on use of functions such as 
ReplacePart, Position, Cases, and Length being used to probe parts of a 
perhaps ragged list.
In another thread use of the Check function (perhaps with Quiet) is 
proposed.  I would think that Check & Quiet will also work with ragged 
arrays.
Anyway, we seem to have found a set of possible approaches.
Thanks,
Joe Gwinn