MathGroup Archive 2000

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

Search the Archive

Re: Evaluate a string


On 10 Sep 2000, Phlip wrote:

> The inner problem. In Perl, for example, you can evaluate a string back
> in thru the Perl parser:
> 
>     my x = 0;
>     eval "x = 3";
>     assert (x == 3);
> 
> I have a string containing Mathematica language code. How do I evaluate
> it as a Mathematica statement?

Use ToExpression[]. 

In[1]:= ToExpression["foo = 3"]

Out[1]= 3

In[2]:= ? foo
Global`foo

foo = 3

> Taking the code out of the string is not an option. (But I could
> conceivably write the string to a file, and then Load or Run this. But
> I can't find a Mathematica command to load a file and execute it.)

That would be the Get[] command, oftentimes abbreviated as the prefix
operator "<<".  The Run[] function tells the kernel to execute an external
operating system command (like system() in C).

-- 
P.J. Hinton
User Interface Programmer                         paulh at wolfram.com
Wolfram Research, Inc.
Disclaimer: Opinions expressed herein are those of the author alone.


  • Prev by Date: Re: Add the Logarithms (error in integral)
  • Next by Date: CellAutoDelete
  • Previous by thread: Evaluate a string
  • Next by thread: Re: Evaluate a string