Re: string-variable-Set-snarl
- To: mathgroup at smc.vnet.net
- Subject: [mg22454] Re: [mg22491] string-variable-Set-snarl
- From: BobHanlon at aol.com
- Date: Wed, 8 Mar 2000 02:21:52 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
changeValue[name_String, value_?NumericQ] :=
ToExpression[name <> "=" <> ToString[value]];
cow = 0.2;
animalname = "cow";
changeValue[animalname, 2];
cow
2
Bob Hanlon
In a message dated 3/4/2000 4:15:07 AM, jfuite at UAlberta.ca writes:
>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?
>