Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

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

  • To: mathgroup at smc.vnet.net
  • Subject: [mg112688] Re: How do I test for existence of a list element? Clarified
  • From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
  • Date: Mon, 27 Sep 2010 05:45:57 -0400 (EDT)
  • References: <i7mq20$ote$1@smc.vnet.net>

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... at 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=


  • Prev by Date: Re: Row or column?
  • Next by Date: Re: calculate vertex of a parabola
  • Previous by thread: How do I test for existence of a list element? Clarified
  • Next by thread: Re: How do I test for existence of a list element? Clarified