Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2012

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

Search the Archive

Re: Using Table and ignoring all errors and warnings?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg125704] Re: Using Table and ignoring all errors and warnings?
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Thu, 29 Mar 2012 03:02:45 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com

On 3/28/12 at 4:57 AM, mikesheppard42 at gmail.com (Mike Sheppard)
wrote:

>I would like to use Table function, but have it ignore any errors or
>warnings and skip that value if one is produced.

>Illustrative Example:

>Table[1/i, {i, -3, 2}]

>I would like the output to just be

>{-(1/3), -(1/2), -1, 1, 1/2}

>with no errors/warnings given as messages and the index that would
>produce a message to just be ignored. It's not just specific to 1/0
>error, but any type of error or warning I want it to ignore it and
>skip to the next value; with the final output being a list of valid
>answers according to expr.

You can achieve what you want (suppress errors) with a
combination of Quiet and Check. For the specific example above,
this works:

In[7]:= Table[
   Quiet@Check[1/i, Hold[Sequence[]]], {i, -3, 2}] // ReleaseHold

Out[7]= {-(1/3),-(1/2),-1,1,1/2}

Check returns the first expression if no error occurs and the
second if an error occurs, Quiet suppresses any error messages.

Hold is being used here, since without it Check would only see
one argument and complain. ReleaseHold at the end removes the
Hold causing the fail result to disappear. Another way of doing
the same thing that might be seen as simpler would be

Table[Quiet@Check[1/i, ""], {i, -3, 2}] /.""->Sequence[]

or

DeleteCases[Table[Quiet@Check[1/i, ""], {i, -3, 2}],""]

If there will be great many instances where the fail condition
occurs, I think the version with Hold, ReleaseHold will be
somewhat faster than either of the other approaches. But with a
small number of cases where the fail condition occurs, it would
be difficult to determine a speed performance difference.




  • Prev by Date: Re: Piecewise ColorFunction
  • Next by Date: Re: Piecewise ColorFunction
  • Previous by thread: Re: Using Table and ignoring all errors and warnings?
  • Next by thread: Re: Using Table and ignoring all errors and warnings?