Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #9278 > unrolled thread

Re: Using Table and ignoring all errors and warnings?

Started byBill Rowe <readnews@sbcglobal.net>
First post2012-03-29 08:06 +0000
Last post2012-03-29 08:06 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Re: Using Table and ignoring all errors and warnings? Bill Rowe <readnews@sbcglobal.net> - 2012-03-29 08:06 +0000

#9278 — Re: Using Table and ignoring all errors and warnings?

FromBill Rowe <readnews@sbcglobal.net>
Date2012-03-29 08:06 +0000
SubjectRe: Using Table and ignoring all errors and warnings?
Message-ID<jl156o$leo$1@smc.vnet.net>
On 3/28/12 at 4:57 AM, mikesheppard42@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.

[toc] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web