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.
ToExpression
$\endgroup$Symbol[]
can't handle symbols with values:x = 5; Symbol["x"]
$\endgroup$OwnValues
of that symbol $\endgroup$Block
$\endgroup$ToExpression
during assignment you have to make MMA understand"x"
isx
which means you have to convert it to an expression (held or not) at some point, and whether you will use something different fromToExpression
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. $\endgroup$