# How do I identify the source of a “Tag times protected” error?

I have some code that looks like

Table[
a = 1;
b = {2, 3}
c = i;,
{i, 2}
]


which gives an error:

Set::write: Tag Times in c {2,3} is Protected. >>

In this case, it is quite clear that there is a semicolon missing after b = {2, 3}, which is causing this error. However, sometimes I encounter this in large code blocks spanning several lines, which is very difficult to debug.

How can I automate this semicolon hunting to make debugging easier?

-

## 1 Answer

Here is a function findBadSets that will find any explicitly bad Set/SetDelayed attempts in a given expression. Simply wrap it around a syntactically complete block of code, or follow the block with // findBadSets and the errors are printed one per row, protected symbol followed by complete left-hand side for each bad Set:

(* your example *) // findBadSets


Code for the function:

SetAttributes[findBadSets, HoldFirst]

findBadSets[expr_] :=
Cases[
Unevaluated @ expr,
(Set | SetDelayed)[bad : head_Symbol[___], _]
/; MemberQ[Attributes@head, Protected] :>
HoldForm[Row[{head, bad}, Spacer[50]]],
-1] // Column

-
WoW, so useful! –  matheorem Nov 15 '13 at 1:32
@matheorem My pleasure. :-) –  Mr.Wizard Nov 15 '13 at 19:03