MathGroup Archive 2005

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

Search the Archive

Re: Newbie Programming question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg53456] Re: Newbie Programming question
  • From: Bill Rowe <readnewsciv at earthlink.net>
  • Date: Wed, 12 Jan 2005 03:41:33 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

On 1/11/05 at 1:32 AM, kjkim at u.washington.edu (Kerry Kim) wrote:

>Is there any way in Mathematica to store/assign a value to a
>variable whose name is in a string?  In other words something like
>this:

>i = 17;
>foo="var" + ToString[i];
>SomeFunctionThatConvertsStringToSymbolName[foo] = 17;

>and then assign some value (say, 23) to the variable whose name is
>contained in the foo string? (i.e. var17=23)

Yes, for example

n = 17; 
foo = StringJoin["var", ToString[n]]; 
Evaluate[ToExpression[foo]] = 23; 
var17

23

But do note this will not work if var17 already has a value assigned to it. Using this code, Mathematica would evaluate var17 and attempt to assign 23 to its value, something that obviously doesn't make sense. So, to assign subsequent values you would first need to clear var17. For example

Clear[var17];
Evaluate[ToExpression[foo]] = 53;
var17

53
--
To reply via email subtract one hundred and four


  • Prev by Date: Re: [Newbie] Ohm, Ampere, Volt units
  • Next by Date: Re: Newbie Programming question
  • Previous by thread: Re: Newbie Programming question
  • Next by thread: Re: Newbie Programming question