2
$\begingroup$

I have a symbol, say x, whose calculated value is a 2nd symbol. How can I set the 2nd symbol to a given value? For example, say the value of x is one of the symbols a, b, or c. If it is a, I want to set a = 10. If it is b, I want to set b = 10. Ditto for c. But, I can use a, b, or c only if I can extract it from x. I want to do something like

x=a
%=10
Out= a=10

To complicate this just a bit, my 2nd "symbols" actually are subscripts, like Subscript[a,1], Subscript[a,1,2], Subscript[a,1,3,4]. I found a partial solution.

x=Subscript[a,3];
Subscript[x[[1]],x[[2]]=10;
Subscript[a,3]
Out(1)= 10

But how do I do this if a has n subscripts? How do I write something like

x=Subscript[a,3,4,2, ..., 5];
Subscript[a,x[[2]], ... , x[[n]]]=10
$\endgroup$
2
  • $\begingroup$ Hold[x = 10] /. OwnValues[x] // ReleaseHold? -- BTW, I avoid subscripts for variables, and most questions about them. But then I've only been using Mathematica for a couple of decades or so. Still, OwnValues might work with them, if x is a regular symbol. Otherwise, maybe DownValues will work. $\endgroup$ – Michael E2 Sep 14 '16 at 2:44
  • $\begingroup$ I just discovered a partial solution. $\endgroup$ – matrixbud Sep 14 '16 at 2:54
1
$\begingroup$

Avoid the hold on the left hand side of an assignment with Evaluate.

x=a
a
Evaluate[x]=10
10
a
10
x = Subscript[b, 1]
Subscript[b, 1]
Evaluate[x] = 2
2
Subscript[b, 1]
2

But why?

$\endgroup$
8
  • $\begingroup$ Why? Because it sounds like the OP wants reference-type variable passing... $\endgroup$ – Myridium Sep 14 '16 at 3:27
  • $\begingroup$ Thank you, Mark. This seems to do the trick. I searched for "Release" in the Documentation Center and it says that Release has been superseded by Evaluate and ReleaseHold. I tried them and Evaluate seems to work the same way but not ReleaseHold. $\endgroup$ – matrixbud Sep 14 '16 at 4:02
  • $\begingroup$ Ah, didn't know that. I'm an old timer, so I still use Release. I'll update the answer. $\endgroup$ – Mark Adler Sep 14 '16 at 4:10
  • $\begingroup$ Note that this will not work again. Once a is set to a number, x then evaluates to a number, as does x=a. $\endgroup$ – Mark Adler Sep 14 '16 at 4:13
  • 1
    $\begingroup$ Sure. You can a=.. However I am still wondering why you are doing this. You may want to ask a new question about what you are actually trying to do. There is probably a better approach for that than this. $\endgroup$ – Mark Adler Sep 14 '16 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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