Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2003
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

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: <b4gjcq$a4k$1@smc.vnet.net>
  • 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
     Symbol["x"] =13
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
    Evaluate[Symbol["x"] ]=13
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

Allan
---------------
Allan Hayes
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:
>
>
> Evaluate[Symbol["x"]]=13;
>
>
> In[14]:=
> x
>
> Out[14]=
> 13
>
> Andrzej Kozlowski
> Yokohama, Japan
> http://www.mimuw.edu.pl/~akoz/
> http://platon.c.u-tokyo.ac.jp/andrzej/
>
>
> 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