MathGroup Archive 1997

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

Search the Archive

Re: Symbols, names, objects

  • To: mathgroup at
  • Subject: [mg6522] Re: [mg6515] Symbols, names, objects
  • From: Tom Wickham-Jones <twj>
  • Date: Sun, 30 Mar 1997 22:23:55 -0500 (EST)
  • Sender: owner-wri-mathgroup at

Murray Eisenberg wrote:
> I need a function, let's call it Store, that takes two strings as
> arguments -- the NAMES of two variables the second ov which variables
> currently has some value -- and that causes the value in that variable
> to be assigned to the symbol.  (I don't care what the result returned
> by the function is; Null will do just fine.)  For example:
>     temp = {1, 2, 3};
>     Store["x", "temp"]
> After evaluating those expressions, the list {1,2,3} of numbers would
> be assigned to the variable x.
> I tried the "obvious" thing:
>    Store[newvar_String, datavar_String] :=
>        (newvar<>"="<>datavar; Null)
> but that does not work.  I've tried various alternatives, such as
> inserting "Evaluate@"<> just before datavar in the above definition,
> but that doesn't do it, either.
> More generally, I'd like Store to be able to accept as second argument
> the string representation of any expression, where such expression,
> when evaluated, give the value to be assigned to newvar.

If you want to turn a string into a symbol you will have to parse it which
you can do with ToExpression.  So one version of Store is...

In[7]:= Store[newvar_String, datavar_String] :=
       ToExpression[ newvar <> "=" <> datavar]
In[8]:=  temp = {1, 2, 3};

In[9]:=  Store["x", "temp"]

Out[9]= {1, 2, 3}

In[10]:= x

Out[10]= {1, 2, 3}

This will fail if the newvar input is not something that take a value
or if the datavar does not represent valid syntax.

In[16]:= Store[ "\"x\"", "foo"]  

Set::setraw: Cannot assign to raw object x.

Out[16]= foo

In[17]:= Store[ "x", "{"]

ToExpression::sntxi: Incomplete expression; more input is needed.

Out[17]= $Failed

In Mathematica the expressions that can take values are symbols 
and expressions with a head that can take a value.  In In[16]
the attempt was made to assign a value to a string which is not permitted.

Tom Wickham-Jones

  • Prev by Date: Re: NDSolve problem
  • Next by Date: Coupled ODEs: not solving [REPOST]
  • Previous by thread: Re: Symbols, names, objects
  • Next by thread: Re: Symbols, names, objects