Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Re: String as a variable name

  • To: mathgroup at smc.vnet.net
  • Subject: [mg100325] Re: String as a variable name
  • From: Szabolcs <szhorvat at gmail.com>
  • Date: Mon, 1 Jun 2009 07:07:06 -0400 (EDT)
  • References: <gvtm9o$g9h$1@smc.vnet.net>

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] = 1 would work in its stead.  It's
simpler and safer.


  • Prev by Date: Re: Reading Fortran Data file
  • Next by Date: Re: MatrixForm affects values instead of only printing ?????
  • Previous by thread: Re: Accessing Dictionary definitions
  • Next by thread: Re: Re: String as a variable name