Variations on my question have been asked before (How to re-assign values to a List of variables?, How to use Table to assign values to variables in a list?, Assigning values to a list of variable names, Pass the list of variables into function to change their values), but I'm not sure if any of them exactly cover my issue.
((#1 = 1) &) /@ {a, b, c}
sets a
, b
, and c
to 1. However, the similar-looking
Do[x = 1, {x, {a, b, c}}]
does nothing. I assume this is because "Do
effectively uses Block
to localize values or variables", as stated in the documentation. But once the values have been set, neither approach works to change their values:
((#1 = #1^2) &) /@ {a, b, c}
gives the error message "Set::setraw: Cannot assign to raw object 1.", and
(If[EvenQ[#1], #1 = #1^2] &) /@ {a, b, c}
returns {Null, Null, Null}
without changing anything. (The equivalent Do
versions don't do anything, as I expected.) Do I need to use Hold
or Unevaluated
or something to get Mathematica to reassign the variables?