This is probably a really simple one, but I'm stumped. I want Symbol["x"]=3 to work just like x=3. However I can't figure out how to get Mathematica to evaluate the Symbol, converting the string to the symbol x, but then not evaluate x.
x = 2
2
Symbol["x"] = 3
Set::write: Tag Symbol in Symbol[x] is Protected. >>
3
Evaluate[Symbol["x"]] = 2
Set::setraw: Cannot assign to raw object 3. >>
2
I know that I can remove the definition, but I have a ton of these and would like to be able to replace their values that have been already set. x=3 doesn't require that I remove the definition of x to set it.
In case it matters, I'm using 10.0.0.
Set @@ Append[ToHeldExpression["x"], 3]orMakeExpression@"x" /. _[s_] :> (s = 4)– Mr.Wizard Aug 28 '14 at 16:53ToExpression["x", StandardForm, Function[s, s = 5, HoldFirst]]– Mr.Wizard Aug 28 '14 at 17:02Clear[x]; Activate[Inactive[Set][Symbol["x"],3]]I agree we can close this question, as a duplicate of How do you programatically load data into symbols?. Then I'll post my answer there. – Jens Aug 28 '14 at 17:48ToExpressionlooks like the right approach.ToHeldExpressionis marked as obsolete. – Mark Adler Aug 28 '14 at 17:58Clear[x]is not useful, since I would actually have to doClear[Symbol["x"]], which runs into exactly the same problem. – Mark Adler Aug 28 '14 at 18:00ToExpressionworks, I was hoping for something that gave finer control over evaluation, e.g. allowing only one step of the evaluation to take place. Control of evaluation has always been a bugaboo for me in using Mathematica. – Mark Adler Aug 28 '14 at 18:04Unprotect[Set]; Set[s_?StringQ, v_] := ToExpression[s, StandardForm, Function[x, x = v, HoldFirst]]; Protect[Set];. Then I can simply"x"=3and it works!Setnormally gives an error if the left hand side is a string, so I think this is ok. For me anyway, this is a natural and useful extension ofSet. – Mark Adler Aug 28 '14 at 18:15Clearonly to get a clean slate for my answer to your question, thinking that you were asking only aboutSethere. – Jens Aug 28 '14 at 18:17Clearis not necessary? The whole point of the question is to not have to clear the definition ofxfirst. If you clear the definition first, then theEvaluateworks fine, since the symbolxevaluated is justx. – Mark Adler Aug 28 '14 at 18:18Clearis not useful"` that is not true; see: (37755) – Mr.Wizard Aug 28 '14 at 18:19Clearworks on strings. In any case, I want to be able to do this without clearing the definitions, and yourToExpressionapproach does the trick. – Mark Adler Aug 28 '14 at 18:20SetI recommend that you use an UpSet on a special head as WReach did here and I did here. Proposal:Set[svar[s_?StringQ], R_] ^:= MakeExpression@s /. _[L_] :> (L = R)nowsvar["x"] = 5works. You should probably thenProtect[svar]to prevent unwanted assignments. – Mr.Wizard Aug 28 '14 at 18:27Clearing variables in your question. Looks like @Mr.Wizard has the thing you need... – Jens Aug 28 '14 at 18:28_?StringQis probably necessary as you may not have a literal string but rather an expression that evaluates to a string. I changed my code above to reflect this. – Mr.Wizard Aug 28 '14 at 18:34