MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

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.
>
>


  • Prev by Date: Graphics saving nightmare
  • Next by Date: Re: Re: comments on Wolfram Alpha
  • Previous by thread: Re: String as a variable name
  • Next by thread: Re: MatrixForm affects values instead of only printing ?????