[Date Index] [Thread Index] [Author Index]
Re: String as a variable name
On May 31, 1:31 pm, "A. B." <functionalcoatings at gmail.com> wrote: > I found it: I forgot to wrap Evaluate around the assignment. This works: > > Symbol["variable"<>"1"]//Evaluate=10 > This is not going to work if variable1 already has a value. Because of how Mathematica evaluates expressions, the only way around this is using the third argument of ToExpression: assignToName[name_String, value_] := ToExpression[name, InputForm, Function[var, var = value, HoldAll]] Assign 1 to variable1 assignToName["variable" <> "1", 1] However, whenever one feels the need to use something like this, it's worth thinking over if variable = 1 would work in its stead. It's simpler and safer.