I want to do the following in Mathematica
Table[p[i], {i, -3, 0}] = Flatten[{Table[0, {i, -3, -1}], 1}]
But I got an error:
Set::write: Tag Table in Table[p[i], {i, -3, 0}] is Protected.
However, it is perfectly fine to do:
{p[-3], p[-2], p[-1], p[0]} = Flatten[{Table[0, {i, -3, -1}], 1}]
Many thanks!
Force the LHS to evaluate into pieces that can be assigned to:
Evaluate[Table[p[i], {i, -3, 0}]] = Flatten[{Table[0, {i, -3, -1}], 1}]
The reason it doesn't work is because Set has attribute HoldFirst. It means that Set[a,stuff] passes symbol a instead of value of a to Set function. As to why it has this attribute, ask yourself: when you do Set[a,stuff], do you want to assign stuff to symbol a, or to the value of a?
In your example, a holds a table of variable names, so you want the value of a and HoldFirst is annoying. However, most of the time a will have a value like 5 and you want a=stuff to assign stuff to symbol a, not to the value 5
A common pattern to get around Holding attributes is the following:
Set@@{Table[p[i], {i, -3, 0}],Flatten[{Table[0, {i, -3, -1}], 1}]}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With