Sometimes it seems I can change the value of an element in a list - sometimes I get a protected error message.
I’m sure this is a common problem error.
Is there a tutorial or general guide on this? Any dodges? Am I missing something fundamental?
It’s difficult to tell what your problem might be without examples.
This is one common mistake people make:
In[1]:= list = {1, 2, 3}
Out[1]= {1, 2, 3}
In[2]:= list[2] = 7
During evaluation of In[2]:= Set::write: Tag List in {1,2,3}[2] is Protected. >>
Out[2]= 7
In[3]:= list[[2]] = 7
Out[3]= 7
In[4]:= list
Out[4]= {1, 7, 3}
The difference here is that list[2] (without the double square brackets) is a function application. This evaluates to:
{1,2,3}[2]=7
or in FullForm:
Set[List[1, 2, 3][2], 7]
which causes the error (since List is a protected system symbol).
On the other hand list[[2]] (double square brackets) is how to specify a part of an expression and = (Set) allows modifications on parts of lists.
The ReplacePart function might be helpful as well here:
http://reference.wolfram.com/mathematica/ref/ReplacePart.html
Thanks both - I’m wondering whether my problems were just being careless on the double brackets.
Also, I had a learning curve on how to alter the value of parameters passed into a function {using Hold}.
Possibly the two apects together got me confused.
So ReplacePart is just a shorthand way to make multiple changes to a list?