Re: Re: How to convert string to variable name
- To: mathgroup at smc.vnet.net
- Subject: [mg39904] Re: Re: How to convert string to variable name
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Tue, 11 Mar 2003 02:36:25 -0500 (EST)
- References: <firstname.lastname@example.org>
- Sender: owner-wri-mathgroup at wolfram.com
A general observation on evaluation may be helpful:
The normal evaluation for an expression of the form
h[e1, e2, ...] = r
is to evaluate r then h then e1, e2... , giving, say,
h*[e1*, e2*, ... ] = r*.
The right hand side, h*[e1*, e2*, ... ], is not evaluated further, even
if there is a stored rule that would transform it - otherwise we would not
be able to overwrite an exising rule.
The rule corresponding to h*[e1*, e2*, ... ] = r* is then stored and r*
(actually, r** the value of r*) is returned as the value of the expression.
This is why the input
does not work as desired: Symbol["x"] is not evaluated to x, and a message
is generated beause Mathematica takes it that we are trying to redefine
Symbol - which is protected.
On the other hand
works because Evaluate cancels the holding and special evaluation of the
left side so that the evaluation steps become
Evaluate[Symbol["x"] ]=13 --> x=3 (store rule) --> 3
hay at haystack.demon.co.uk
Voice: +44 (0)116 241 8747
Fax: +44 (0)870 164 0565
"Andrzej Kozlowski" <akoz at mimuw.edu.pl> wrote in message
news:b4gjcq$a4k$1 at smc.vnet.net...
> It does work, but you need to evaluate the left hand side:
> Andrzej Kozlowski
> Yokohama, Japan
> On Sunday, March 9, 2003, at 07:28 pm, Bob Harris wrote:
> > I wrote:
> >>> ... for a given string, how can I assign to the variable whose name
> >>> is
> >>> defined by that string?
> > and Hermann Schmitt replied:
> >> I would do it the following way:
> >> ToExpression["z = ....."];
> > Thanks, that appears to be what I want. I just have to build a string
> > that
> > contains the entire assignment.
> > A couple people suggested using Symbol, but I don't see how that
> > would
> > work. For example,
> > Symbol["x"] = 13
> > doesn't create a symbol x with value 13. Instead, it simply produces
> > an
> > error message informing me that "Tag Symbol in Symbol["x"] is
> > Protected."
> > Thanks,
> > Bob H
Prev by Date:
Re: Symbols and Lists
Next by Date:
RE: Re: Symbols and Lists
Previous by thread:
Re: How to convert string to variable name
Next by thread:
pattern matching with repeated named patterns