I would like to set values to a list of variables, like so:
list[[1]] = 2
and if list[[1]] is a, then a will now be equal to two. How can I achieve this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Well, let’s try it naively:
Make a list:
It’s as we expect it:
Set the first element to 2:
That doesn’t affect a:
Start again:
The problem is that Set (=) has
HoldFirstas one of its attributes , i.e., it doesn’t evaluate its first argument which is the lefthand side, and the assignment is to the list and not to the variable that’s in that location. But you can force evaluation usingEvaluate:Now the list seems to be the same as before:
but that’s only because a is still there and has gotten the value of 2 (in the previous version a was replaced by 2):
If you now set a to 3 you’ll see that that changes list too:
EDIT
Perhaps more close to the wording of your question, you could
MapSetover the list: