2
$\begingroup$

This question already has an answer here:

I have a list of variables (say a, b, c, d, e) that I want to assign in a single command.

{a,b,c,d,e} = {1,2,3,4,5}

would work, but it is cumbersome to type {a,b,c,d,e} each time. Is there a way I can create a table that holds references to these variables, and whenever I assign to this table, the actual variables a,b,c,d,e will get assigned?

Sorry for the question - I am new to Mathematica and do not even know the basic terms. If you guessed I have a C++ background, you are right.

$\endgroup$

marked as duplicate by Mr.Wizard Feb 7 '15 at 3:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ Is something like that ok: list := {a, b, c, d, e}; Unevaluated[list = {1, 2, 3, 4, 5}] /. OwnValues[list]? $\endgroup$ – Kuba Jan 30 '15 at 9:40
  • $\begingroup$ related: 40094 $\endgroup$ – Kuba Jan 30 '15 at 9:46
  • $\begingroup$ less related: 70250 $\endgroup$ – Kuba Jan 30 '15 at 9:54
  • $\begingroup$ Proposed duplicate: (10322). Also related: (6511) (and see links below that one) $\endgroup$ – Mr.Wizard Jan 30 '15 at 10:16
  • $\begingroup$ The functional programmer might wonder, why would you want to do that ? ;) $\endgroup$ – image_doctor Jan 30 '15 at 10:24
4
$\begingroup$

Most user friendly approach, with function:

mySet[l_] := {a, b, c, d, e} = l

I'm not sure what is the general goal, but here's one way with UpValues:

table /: Set[table, l_] := ({a, b, c, d, e} = l);

table = Range[6, 10]; 
{a, b, c, d, e}
{6, 7, 8, 9, 10}
table = Range[1, 5];
{a, b, c, d, e}
{1, 2, 3, 4, 5}

Different approach:

list := {a, b, c, d, e};  
Unevaluated[list = {1, 2, 3, 4, 5}] /. OwnValues[list]
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.