jashonder
(John Shonder)
1
Suppose I have a list of variables:
vlist={a,b,c}
I want to set a=1, b=2, c=3, but the following doesn’t work:
vlist={1,2,3}
After the previous statement, vlist = {1,2,3} but the values of a, b, and c are unassigned.
Of course the following is trivial:
{a,b,c}={1,2,3}
but I want the variables contained in vlist to be selected programatically.
Can anyone suggest a path forward?
shenghuiy
(Shenghui Yang)
2
Maybe
With[{v = vlist}, v = Range[3]]
Hi John,
Maybe
Remove[a, b, c, vlist];
vlist = Unevaluated /@ {a, b, c};
MapThread[Set, {vlist, {1, 2, 3}}];
{a, b, c}
(* {1, 2, 3} *)
jashonder
(John Shonder)
4
Thank you Rohit, this works.
Hello John,
I guess one sees this effect because Set has the attribute HoldFirst - for obvious reasons! Consequently you can do it simply like so:
Remove[a, b, c, vlist];
vlist = {a, b, c};
Evaluate[vlist] = {1, 2, 3}