Services & Resources / Wolfram Forums
MathGroup Archive
*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
  • Subject: [mg53443] Re: [mg53436] Newbie Programming question
  • From: "Wolf, Hartmut" <Hartmut.Wolf at>
  • Date: Wed, 12 Jan 2005 03:41:13 -0500 (EST)
  • Sender: owner-wri-mathgroup at

>-----Original Message-----
>From: Kerry Kim [mailto:kjkim at] 
To: mathgroup at
>Sent: Tuesday, January 11, 2005 7:32 AM
>To: mathgroup at
>Subject: [mg53443] [mg53436] Newbie Programming question
>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)

This is not quite so easy, and for the beginning I'd not recommend to
persue such ideas (there are clearer choices for indirection). However,
you might like to observe this:

In[1]:= i = 17;
        fooString = "var" <> ToString[i];

In[3]:= fooSym = Symbol[fooString]
Out[3]= var17

In[4]:= Evaluate[fooSym] = 17
Out[4]= 17

In[5]:= var17
Out[5]= 17


In[6]:= MakeExpression[fooString]
Out[6]= HoldComplete[var17]

In[7]:= Unevaluated @@ MakeExpression[fooString]
Out[7]= Unevaluated[var17]

With[{variable = Unevaluated @@ MakeExpression[fooString]}, variable =
Out[8]= 23

In[9]:= var17
Out[9]= 23

In[10]:= fooString // InputForm
Out[10]//InputForm= "var17"

Another possibility would be to work with unevaluated symbols as values
(to be referenced):

In[16]:= Quit[]
In[1]:= i = 17;
        fooString = "var" <> ToString[i];

(fooSym = Unevaluated@Unevaluated[#]) &@ Symbol[fooString]
Out[3]= Unevaluated[var17]

In[4]:= ?fooSym
fooSym = Unevaluated[var17]

In[5]:= (# = 17) &@ fooSym
Out[5]= 17

In[6]:= var17
Out[6]= 17

In[7]:= (# = 23) &@ fooSym
Out[7]= 23

In[8]:= var17
Out[8]= 23

In[9]:= fooSym
Out[9]= Unevaluated[var17]

Hartmut Wolf

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