       Re: Newbie Programming question

• To: mathgroup at smc.vnet.net
• Subject: [mg53443] Re: [mg53436] Newbie Programming question
• From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
• Date: Wed, 12 Jan 2005 03:41:13 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```
>-----Original Message-----
>From: Kerry Kim [mailto:kjkim at u.washington.edu]
To: mathgroup at smc.vnet.net
>Sent: Tuesday, January 11, 2005 7:32 AM
>To: mathgroup at smc.vnet.net
>Subject: [mg53443] [mg53436] Newbie Programming question
>
>Hello,
>
>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)
>
>Thanks!
>
>

Kerry,
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:= i = 17;
fooString = "var" <> ToString[i];

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

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

In:= var17
Out= 17

Now

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

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

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

In:= var17
Out= 23

In:= fooString // InputForm
Out//InputForm= "var17"

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

In:= Quit[]

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

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

In:= ?fooSym
Global`fooSym
fooSym = Unevaluated[var17]

In:= (# = 17) &@ fooSym
Out= 17

In:= var17
Out= 17

In:= (# = 23) &@ fooSym
Out= 23

In:= var17
Out= 23

In:= fooSym
Out= 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