Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: Newbie Programming question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg53624] Re: Newbie Programming question
  • From: David Bailey <dave at Remove_Thisdbailey.co.uk>
  • Date: Fri, 21 Jan 2005 06:35:28 -0500 (EST)
  • References: <cs2pme$acj$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Bill Rowe wrote:
> 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
> 
The problem is, of course, if var17 has a value he will not know in 
general what variable to clear! Something like this will work 
regardless, however:

ToExpression[foo<>"="<>ToString[53]]

David Bailey
dbaileyconsultancy.co.uk


  • Prev by Date: Re: ShowLegend....
  • Next by Date: Re: TableForm and MatrixForm
  • Previous by thread: Re: Newbie Programming question
  • Next by thread: mathematica installation problem