Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I test for existence of a list element? Clarified

408 views
Skip to first unread message

Joseph Gwinn

unread,
Sep 26, 2010, 2:43:12 AM9/26/10
to
I have an application where I am bouncing around in a list, and may
accidentally ask for an element beyond the end of the list, which causes
Mathematica to complain and balk, preventing completion.

Is there any way to test for the existence of a list element without
provoking complaint or balking should the list item fail to exist?

Many of the suggestions made offline in response to the above imply that
I wasn't clear enough, so here is some expansion:

All the list operations like MemberQ tell you if a member of the list
has some property or not. But it does not solve the problem of telling
if a slot (part) exists or not.

For instance:

list={a, b, c, d}; x=list[[60]]

Mathematica will complain that "list[[60]]" does not exist, which is true.

Mathematica's specific complaint is "Part::partw: Part 60 of {a,b,c,d} does not
exist." Turning the error message off does not solve the problem.

The question is how to test if list[[60]] exists without losing control
if it does not.

Other than by doing the usual arithmetic and test involving Length[list].


It may be that no such test exists, which would also be useful to know.


Thanks,

Joe Gwinn

Sjoerd C. de Vries

unread,
Sep 27, 2010, 5:45:52 AM9/27/10
to
I don't know what your problem with using Length is, but if you really
need a specific test function you can easily roll it yourself:

indexQ[index_Integer, list_List] := index <= Length[list]

Cheers -- Sjoerd

On Sep 26, 8:43 am, Joseph Gwinn <joegw...@comcast.net> wrote:
> I have an application where I am bouncing around in a list, and may
> accidentally ask for an element beyond the end of the list, which causes
> Mathematica to complain and balk, preventing completion.
>
> Is there any way to test for the existence of a list element without
> provoking complaint or balking should the list item fail to exist?
>
> Many of the suggestions made offline in response to the above imply that
> I wasn't clear enough, so here is some expansion:
>
> All the list operations like MemberQ tell you if a member of the list
> has some property or not. But it does not solve the problem of telling
> if a slot (part) exists or not.
>
> For instance:
>
> list={a, b, c, d}; x=list[[60]]
>

> Mathematica will complain that "list[[60]]" does not exist, which is true=
.
>
> Mathematica's specific complaint is "Part::partw: Part 60 of {a,b,c,d} do=

Andrzej Kozlowski

unread,
Sep 27, 2010, 5:47:18 AM9/27/10
to
The function Check is (probably) what you are looking for.

list == {a, b, c, d};

x == Check[list[[60]], {}]
During evaluation of In[69]:== Part::partw:Part 60 of {a,b,c,d} does not exist. >>
{}

You can use whatever you like instead of {}. If you don't want to see the error message you don't have to:

x==Quiet[Check[list[[60]],{}]]
{}

Andrzej Kozlowski


On 26 Sep 2010, at 08:42, Joseph Gwinn wrote:

> I have an application where I am bouncing around in a list, and may
> accidentally ask for an element beyond the end of the list, which causes
> Mathematica to complain and balk, preventing completion.
>
> Is there any way to test for the existence of a list element without
> provoking complaint or balking should the list item fail to exist?
>
>
>
> Many of the suggestions made offline in response to the above imply that
> I wasn't clear enough, so here is some expansion:
>
> All the list operations like MemberQ tell you if a member of the list
> has some property or not. But it does not solve the problem of telling
> if a slot (part) exists or not.
>
> For instance:
>

> list=={a, b, c, d}; x==list[[60]]
>
> Mathematica will complain that "list[[60]]" does not exist, which is true.
>
> Mathematica's specific complaint is "Part::partw: Part 60 of {a,b,c,d} does not

Joseph Gwinn

unread,
Sep 29, 2010, 4:10:54 AM9/29/10
to
In article <i7pp4g$l6e$1...@smc.vnet.net>,

"Sjoerd C. de Vries" <sjoerd.c...@gmail.com> wrote:

> I don't know what your problem with using Length is,

Too obvious, but sometimes clumsy to use. And I figured that
Mathematica must have a more direct approach, hidden in some dusty
corner. It appears that Check such a function, and I had not heard of
it before.

Thanks,

Joe Gwinn


> ... but if you really

Joseph Gwinn

unread,
Sep 29, 2010, 4:11:05 AM9/29/10
to
In article <i7pp76$l9j$1...@smc.vnet.net>,
Andrzej Kozlowski <ak...@mimuw.edu.pl> wrote:

> The function Check is (probably) what you are looking for.
>
> list == {a, b, c, d};
>
> x == Check[list[[60]], {}]
> During evaluation of In[69]:== Part::partw:Part 60 of {a,b,c,d} does not
> exist. >>
> {}
>
> You can use whatever you like instead of {}. If you don't want to see the
> error message you don't have to:
>
> x==Quiet[Check[list[[60]],{}]]
> {}

This looks quite suitable. I knew that Mathematica must have such a
function, somewhere in those thousands of functions and variants.

Thanks,

Joe Gwinn

0 new messages