Re: Re: String as a variable name

*To*: mathgroup at smc.vnet.net*Subject*: [mg100395] Re: [mg100325] Re: String as a variable name*From*: Leonid Shifrin <lshifr at gmail.com>*Date*: Tue, 2 Jun 2009 06:47:26 -0400 (EDT)*References*: <gvtm9o$g9h$1@smc.vnet.net> <200906011107.HAA25906@smc.vnet.net>

Hi, > the only way around this is using the third argument of ToExpression just for completeness, there is another way: assignToNameStr[name_String, value_] := ToExpression["Set[" <> name <> "," <> ToString[FullForm[value]] <> "]"]; However, the solution of Szabolcs is preferable in several ways. My feeling is that while the string representation is sometimes a useful trick, generally the ToString-ToExpression cycles should be avoided whenever possible. Regards, Leonid > 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] = 1 would work in its stead. It's > simpler and safer. > >

**References**:**Re: String as a variable name***From:*Szabolcs <szhorvat@gmail.com>