RE: function mySet[], Strings to Symbols
- To: mathgroup at smc.vnet.net
- Subject: [mg36120] RE: [mg36096] function mySet[], Strings to Symbols
- From: "David Park" <djmp at earthlink.net>
- Date: Thu, 22 Aug 2002 04:32:57 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Jan,
My first choice would be to simply write
a = {2,3,4,5,6}
However, let's assume that you have a good reason to start with strings. The
way to prevent Symbol[s] from immediately evaluating to a previously set
value is to first Clear it. Fortunately, Clear allows strings as arguments.
So, I might use the following method.
mySet[s_String, value_] := (Clear[s]; Evaluate[Symbol[s]] = value)
a = {1, 2, 3, 4, 5};
"a" ~mySet~ {2, 3, 4, 5, 6};
a
{2, 3, 4, 5, 6}
Note that ~mySet~ is the Mathematica method for using mySet as an infix
operator.
David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/
From: Prof. Korvink [mailto:korvink at informatik.uni-freiburg.de]
To: mathgroup at smc.vnet.net
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