I've got a situation where I have, say 4 symbols, a, b, c and d. This is a simplification of the issue I've been working with. Let's say I assign numeric values to these symbols:a=1; b=2; c=3; d=4. I now create a list: myList:={a,b,c,d}. I'd like to be able to say: ( # = 5 ) & /@ myList to assign the value 5 to a, b, c and d. The code works as long as a-d are unassigned. Is there a way to do this, or am I trying to abuse the language?
Tell me more
×
Mathematica Stack Exchange is a question and answer site for
users of Mathematica. It's 100% free, no registration required.
|
|
||||
|
This seems to work:
Now, try evaluating |
|||||||||||||||||
|
|
You could use
|
|||
|
|
|
If you insist on working with your list where you assemble variables, this will do it:
For example:
|
|||||||||
|
|
One could use
or:
|
|||
|
|
|
We can define a new "variable container" that can be used to assign the same value to multiple variables:
It is used like this:
|
|||
|
|

Trace[myList:={a,b,c,d}]and ofTrace[myList={a,b,c,d}]. The former is a mistake while the latter attempts to issue a sequence ofSetassignments1=5,2=5, ...,4=5.) – whuber Jan 19 '12 at 15:01Scan[]instead ofMap[](that is,/@) for multiple assignments. – J. M.♦ Jan 19 '12 at 15:01