Re: string-variable-Set-snarl
- To: mathgroup at smc.vnet.net
- Subject: [mg22489] Re: [mg22491] string-variable-Set-snarl
- From: Hartmut Wolf <hwolf at debis.com>
- Date: Wed, 8 Mar 2000 02:22:29 -0500 (EST)
- Organization: debis Systemhaus
- References: <200003050524.AAA14078@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
James Fuite and Tania Nordli schrieben:
>
> Dear Advanced Mathematica Users,
>
> The following is a short and perhaps trivial problem that has caused me
> some frustration. I cannot write a program that allows me to change the
> value of any given variable if given the name of the variable as a
> string. What follows is an equivalent situation. Let
>
> In[3]= {cow = Random[], emu = Random[], ant = Random[]}
> Out[3]= {0.2, 0.3, 0.4}
>
> . . . . some selection process . . . .
>
> In[25]= animalname
> Out[25]= cow
>
> In[25]= Head[animalname]
> Out[25]= String
>
> Given a string, here "cow", corresponding to a variable, here cow, I
> cannot clear or set a value for the variable itself. Of course I cannot
> use Clear[cow] or cow = 2 because I do not know which variable will be
> chosen, as represented by the variable animalname. My lame attempts
> such as Clear[animalname] or ToExpression[animalname] = 2 only fail to
> affect cow - it seems that I am caught trying to work on the wrong side
> of the equals sign (Set)! My goal would be a line such as:
>
> In[28]= ToExpression[animalname]
> Out[28]= 2
>
> which I think would imply that cow = 2 and has thus been affected. Who
> can guide me from line 25 to line 28? What are the general
> considerations?
>
> Sincerely,
> James J. Fuite.
Dear James,
normally I would not recommend doing that, but there may be occasions where
this is applicable, namely when your symbols (variables) originate outside of
Mathematica or are partially treated outside.
Here are a few functions to treat strings "as variables":
In[1]:=
makesymbol = (((mysymbol[#1] = Unevaluated at Unevaluated[#2]) &)[#,
Symbol[#]] &);
In[2]:=
mysymbol[s_String] := (Clear[s]; makesymbol[s])
In[3]:=
mysymbol[s_Symbol] := mysymbol[ToString[Unevaluated[s]]]
In[4]:=
Attributes[setsymbol] = {HoldFirst};
In[5]:=
setsymbol[s_, rhs_] := ((#1 = #2) &)[mysymbol[Unevaluated[s]], rhs]
You only use the function setsymbol. Give it the name (i.e. a string) of a
symbol (already existing or not) and it sets that symbol referred to,
irrespectively of whether it already has a value or not.
In[6]:= setsymbol["cow", 11];
In[7]:= cow
Out[7]= 11
In[8]:= setsymbol["cow", 12];
In[9]:= cow
Out[9]= 12
In[10]:= Clear[cow]
In[11]:= cow
Out[11]= cow
For your convenience it's tolerant enough as to also accept the symbol
itself:
In[12]:= setsymbol[aardvark, 4];
In[13]:= aardvark
Out[13]= 4
In[14]:= setsymbol[aardvark, 7];
In[15]:= aardvark
Out[15]= 7
In[16]:= setsymbol["aardvark", 8];
In[17]:= aardvark
Out[17]= 8
In[18]:= Clear["aardvark"]
In[19]:= aardvark
Out[19]= aardvark
That is standard functionality, you can also Clear a symbol by its name.
Now if you have a reference to a name:
In[21]:= animalname = "ant";
In[22]:= setsymbol[animalname, 99];
In[23]:= animalname
Out[23]= 99
you can't use it directly, since animalname will be redefined (as in
ordinary Set)
However if you do
In[25]:= ant = 88;
In[26]:= animalname = "ant";
In[27]:= setsymbol[Evaluate[animalname], 99];
In[28]:= ant
Out[28]= 99
In[29]:= animalname
Out[29]= "ant"
All this works via a stored association of names with unevaluated symbols
to be inserted at the lhs of Set (within setsymbol).
In[30]:= Information["mysymbol", LongForm -> False]
"Global`mysymbol"
mysymbol["aardvark"] = Unevaluated[aardvark]
mysymbol["animalname"] = Unevaluated[animalname]
mysymbol["ant"] = Unevaluated[ant]
mysymbol["cow"] = Unevaluated[cow]
mysymbol[s_String] := (Clear[s]; makesymbol[s])
mysymbol[s_Symbol] := mysymbol[ToString[Unevaluated[s]]]
As you see this association is not cleared when the variables themselves are
cleared; a possible advantage is that you always overlook the names used.
Otherwise you would have to supply your own function "clearsymbol".
I hope this will help you, and not cause too much trouble.
Kind regards, Hartmut
- References:
- string-variable-Set-snarl
- From: James Fuite and Tania Nordli <jfuite@UAlberta.ca>
- string-variable-Set-snarl