1
$\begingroup$

Suppose I have an InputField of type String wherein I want to enter a symbol name that I want a later Button to assign values to. That button would then do something like:

Evaluate[ToSymbol[string]]=stuff

But if the symbol name that is entered already has been assigned a value, this fails because the left-hand-side becomes the value, to which it is not valid to assign a new value. How do I convert the string to a symbol without evaluating that symbol?

$\endgroup$
3
  • 1
    $\begingroup$ I think your question is fully answered in the linked topic but let me know if you disagree with marking it a duplicate. $\endgroup$
    – Kuba
    Commented Sep 17, 2019 at 6:16
  • $\begingroup$ Agreed. Thank you for pointing me to that linked topic. I am not sure why my searches didn't pull that one up... $\endgroup$ Commented Sep 17, 2019 at 21:25
  • $\begingroup$ No worries, it is really tough to find a duplicate unless you know the answer or have seen it. $\endgroup$
    – Kuba
    Commented Sep 18, 2019 at 6:23

1 Answer 1

1
$\begingroup$

Use ToExpression with its three arguments form and construct the Set programmatically:

   varname="test";
   value=RandomReal[];
   Set @@ Append[ToExpression[varname, InputForm, Hold], value]

Can you explain why you want to do this? I see many potential problems when you allow users of your gui to do this, they could e.g. accidentially overwrite any of the symbols you have defined in your code...

$\endgroup$
1
  • $\begingroup$ It is a large, multi-tabbed interface that will be part of a Package. It focuses on nonlinear fitting of experimental data to differential rate equations. I need the user, when the fit is done, to be able to output the results to their own variable. I also need the user to be able to output the state of a bunch of internal variables so that they can reuse the model set parameters when fitting at a later date. Overwriting symbols in my code is not a concern because nearly all of those symbols are local, and I will be adding a way to ensure that the symbol name input is global. $\endgroup$ Commented Sep 17, 2019 at 21:29

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