I am using a function defined with a Module[]. In the Module I am creating a new variable name using a Symbol[] function. I would like to reuse the created symbol outside the function call, namely here: varf in the following lines of code:

f[a_] := Module[{var}, var = a; vart = 44; Symbol["var" <> "f"] = 3]

The function returns 3, if I call vart I get 44. If I call var I don't get value of a. If I call varf I get varf.

Can somebody help?

  • $\begingroup$ Have you noticed "Set::write: Tag Symbol in Symbol[varf] is Protected."? Anyway, take a look at How to create symbols from strings and set values for them? $\endgroup$ – Kuba Nov 18 '18 at 15:16
  • $\begingroup$ Yes, that error popped out, but when I called Context[varf] I got Global for a response. $\endgroup$ – brnady Nov 18 '18 at 16:01
  • $\begingroup$ Because it was created when you called Context. $\endgroup$ – Kuba Nov 18 '18 at 16:21
  • $\begingroup$ So you are suggesting that I replace all Symbol[] with RawBox[] and whenever I have a value assignment that I add a wrapper ToExpression@MakeBoxes[...] around the assignment command? $\endgroup$ – brnady Nov 19 '18 at 11:00
  • $\begingroup$ I am suggesting that when you want to assign a value to a symbol generated from a string, then read the linked topic. If you should do this depends of your use case, it is usually better to use Indexed as opposed to symbol generation. $\endgroup$ – Kuba Nov 19 '18 at 12:04

Browse other questions tagged or ask your own question.