# Generate assignment code to symbols from string

I want the function to take in string, then assign a value to its corresponding symbol. An implementation using ToExpression is

mySet[str_String, val_] :=
ToExpression["Set[" <> str <> "," <> ToString[val] <> "]"]


How can I do it without ToExpression? I wonder how this can be done with purely evaluation control.

I have read the following posts: Generating assignments and transforming code inside held forms when generating code

How do I evaluate only one step of an expression?

I think the main problem is to evaluate Symbol["x"] partially to x(it can have OwnValues), which can't be achieved with first post's code; and second post's code is indeed returning a HoldForm expression, leaving me generally the same problem.

• @Kuba what if I want to avoid the use of ToExpression – vapor Jun 2 '16 at 7:33
• I don;t think you can, since you are starting with a string and Symbol[] can't handle symbols with values: x = 5; Symbol["x"] – Kuba Jun 2 '16 at 7:36
• @Kuba I am also curious if there is any method that can temporarily suspend the OwnValues of that symbol – vapor Jun 2 '16 at 7:38
• @Kuba yes, I ended up with the same problem with Block – vapor Jun 2 '16 at 7:41
• The problem is, even if you want to avoid ToExpression during assignment you have to make MMA understand "x" is x which means you have to convert it to an expression (held or not) at some point, and whether you will use something different from ToExpression doesn't matter, you will effectively do this. E.g. you can export "x" as a text file and import/get as expression, but deeper this will still be ToExpression. – Kuba Jun 2 '16 at 7:47

Since you referenced How do I evaluate only one step of an expression? I might use:

(* step loaded from referenced Q&A *)

mySet[str_String, val_] := step @ Symbol[str] /. _[s_Symbol] :> (s = val)


Now:

x = 5;

mySet["x", 7];

x

7


(This also makes use of Injecting a sequence of expressions into a held expression.)

For clarity the above is trying to answer in what I think is the spirit of your question.
Pragmatically I usually(1)(2)(3) use MakeExpression, see e.g. my answer to the closely related:

• Wow this is really clever! – vapor Jun 2 '16 at 7:57
• While MakeExpression is not exactly ToExpression I feel like it's the same in this context. Nontheless P function is mindblowing! – Kuba Jun 2 '16 at 8:09
• @Kuba That's why I did not use MakeExpression in my original answer, but I thought it best to include for reference, as well as your own lovely RawBoxes example. You are referring to the code of my step function? – Mr.Wizard Jun 2 '16 at 8:11
• @Mr.Wizard Yes, but I should have said mind-cracking or something because I still don't fully understand what's happening and how had you figured it out :) – Kuba Jun 2 '16 at 8:15
• @Kuba I will admit that despite its brevity that code took a lot of thinking on my part! The (P = (P = thing is self-redefinition a la (2676) to avoid spitting out the verbatim unevaluated expression. (By the way I just rewrote my answer to keep it more focused, yet hopefully reference what needs referencing for completeness. Let me know if you agree.) – Mr.Wizard Jun 2 '16 at 8:19