Re: function mySet[]
- To: mathgroup at smc.vnet.net
- Subject: [mg36121] Re: [mg36096] function mySet[]
- From: Omega Consulting <omega_consulting at yahoo.com>
- Date: Thu, 22 Aug 2002 04:32:58 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
At 04:52 AM 8/21/2002, 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.
There is a much simpler way to do this if you want to use the symbol. Just
have the function Hold to argument.
Attributes[mySet] = {HoldFirst};
mySet[s_Symbol] := (s = {2, 3, 4, 5, 6})
If you insist on using a string, then it can be done by using a
little-known form of ToExpression.
mySet[s_String] :=
ReleaseHold[
Map[mySet, (* uses definition above *)
ToExpression[s, InputForm, Hold]
]
]
--------------------------------------------------------------
Omega Consulting
"The final answer to your Mathematica needs"
Spend less time searching and more time finding.
http://www.wz.com/internet/Mathematica.html