|
[Date Index]
[Thread Index]
[Author Index]
Re: Newbie Programming question
- To: mathgroup at smc.vnet.net
- Subject: [mg53438] Re: Newbie Programming question
- From: "John Jowett" <John.Jowett at cern.ch>
- Date: Wed, 12 Jan 2005 03:41:08 -0500 (EST)
- Organization: CERN
- References: <crvtn6$144$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
"Kerry Kim" <kjkim at u.washington.edu> wrote in message
news:crvtn6$144$1 at smc.vnet.net...
> 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!
>
One possibility using StringJoin (StringJoin["a","b"} is also written as
"a"<>"b"):
i=17;
foo = "var" <> ToString[i];
ToExpression[foo <> "=23"]
Since you describe yourself as a "newbie", perhaps you will not mind my
suggesting an alternative approach. I remember that when I came to
Mathematica from other programming languages, I often wanted to do the same
sort of thing.
Now I would find it more natural (and simpler) to define a function to store
this information:
i=17;
var[i]=23
Note that this defines the value of the function for a particular argument
(17) because I did not write var[i_].
Also note that var is not a list (there is no need to define var[1], say).
And of course it may be convenient to use non-integer arguments, e.g.,
var["North"]=29
To find out what var contains, type
??var
John Jowett
Prev by Date:
Re: Newbie Programming question
Next by Date:
Re: mathematica installation problem
Previous by thread:
Re: Newbie Programming question
Next by thread:
Re: Newbie Programming question
|