1
$\begingroup$

I want to run an iteration where at each step I check whether I obtain an error message, and if I do, I want to not store that result and move on to the next iteration instead. Here's an example of how it's working using a Do Loop:

Do[Quiet[If[Check[i^0, error] == error, Continue[]]]; Print[i], {i, {1, 0, 2, 0, 1}}]; 
1
2
1

Now, I would like to do something similar using Table instead, and I was thinking of doing something like this:

Table[If[Check[i^0, err] == err, Nothing, i], {i, {1, 0, 2, 0, 1}}]

but this does not work. The output I get is:

{If[1 == err, Nothing, i], If[1 == err, Nothing, i], If[1 == err, Nothing, i]}

If I try doing something that does not involve the function Check, like this:

Table[If[i == 2, Nothing, i], {i, 1, 6}]
{1, 3, 4, 5, 6}

that does work. Is there a way to make Table work with Check in it?

$\endgroup$

1 Answer 1

1
$\begingroup$

Use ===

data = Table[If[Check[i^0, err] === err, Nothing, i], {i, {1, 0, 2, 0, 1}}]

Mathematica graphics

You should have used === in your Do loop also. But there because of Continue you did not notice as it was starting new loop each time.

You can write the Table like this also, which is little bit more clear.

data = Table[z = Quiet@Check[i^0, err]; 
  If[z === err, Nothing, i], {i, {1, 0, 2, 0, 1}}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.