5
$\begingroup$

I am trying to create a list of conditions using Table[] in the following way:

Range[100]/.Table[t_ /; t > k && Mod[t, k] == 0 -> "",{k,2,10}] 

but the result does not change the index value k from 2 to 10.

$\endgroup$

1 Answer 1

6
$\begingroup$

Inject table iterator values into held expressions with With:

Range[100] /. 
 Table[With[{k = k}, t_ /; t > k && Mod[t, k] == 0 -> ""], {k, 2, 10}]
(*
{1, 2, 3, "", 5, "", 7, "", "", "", 11, "", 13, "", "", "", 17, "", \
19, "", "", "", 23, "", "", "", "", "", 29, "", 31, "", "", "", "", \
"", 37, "", "", "", 41, "", 43, "", "", "", 47, "", "", "", "", "", \
53, "", "", "", "", "", 59, "", 61, "", "", "", "", "", 67, "", "", \
"", 71, "", 73, "", "", "", "", "", 79, "", "", "", 83, "", "", "", \
"", "", 89, "", "", "", "", "", "", "", 97, "", "", ""}
*)

See "Possible Issues" in the docs for Table (second-to-last example group).

$\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.