Re: function mySet[]
- To: mathgroup at smc.vnet.net
- Subject: [mg36122] Re: [mg36096] function mySet[]
- From: Andrzej Kozlowski <andrzej at lineone.net>
- Date: Thu, 22 Aug 2002 04:32:59 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
One simple approach is to use Module: In[1]:= a = { 1, 2, 3, 4, 5 }; In[2]:= mySet[ s_String ] :=Module[{t=Symbol[s]},t={ 2, 3, 4, 5, 6 }] In[3]:= mySet[ SymbolName[ Unevaluated[a] ] ]; In[4]:= a Out[4]= {1,2,3,4,5} Note that "Unevaluated" is needed since SymbolName evaluates its input. Andrzej Kozlowski Toyama International University JAPAN On Wednesday, August 21, 2002, at 10:52 AM, Prof. Korvink wrote: > I am struggling with a problem: > > I would like to have a function mySet[] that takes a string argument > that is the correct string name of a "variable" and is able to > make assignments to that variable. Naively: > > a = { 1, 2, 3, 4, 5 }; > mySet[ s_String ] := > Symbol[ s ] = { 2, 3, 4, 5, 6 }; (* ERRONEOUS! *) > mySet[ SymbolName[ a ] ]; > a > > should give: > > { 2, 3, 4, 5, 6 } > > The problem is that Symbol[ s ] immediately evaluates to a List > and I cannot for the love of anything generate an expression that > evaluates to the actual unevaluated symbol. > I have tried many variants with HoldAll and so on, with no luck, > hopefully only out of ignorance. > > What sometimes works (but circumstances is erratic!) is to do a: > > mySet[ s_String ] := > Evaluate[ToExpression[StringJoin[s,"={2,3,4,5,6}"]]] > > I do not find this a clean way to do things. Can anyone offer some > insight and perhaps a solution? > > Jan Korvink > > > >