Message Boards Message Boards

0
|
4071 Views
|
3 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Where to find guidance on when/how lists/array/vector elements altered.

Posted 11 years ago
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?
POSTED BY: William Stewart
3 Replies
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?
POSTED BY: William Stewart
Posted 11 years ago
The ReplacePart function might be helpful as well here:

http://reference.wolfram.com/mathematica/ref/ReplacePart.html
POSTED BY: Peter Fleck
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.
POSTED BY: Arnoud Buzing
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract