MathGroup Archive 2000

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

Search the Archive

Re: Evaluate a string

  • To: mathgroup at
  • Subject: [mg25174] Re: Evaluate a string
  • From: Jens-Peer Kuska <kuska at>
  • Date: Tue, 12 Sep 2000 21:24:20 -0400 (EDT)
  • Organization: Universitaet Leipzig
  • References: <8pfe7f$>
  • Sender: owner-wri-mathgroup at


> The inner problem. In Perl, for example, you can evaluate a string back
> in thru the Perl parser:


"ToExpression[input] gives the expression obtained by interpreting
strings or \
boxes as Mathematica input. ToExpression[input, form] uses
interpretation \
rules corresponding to the specified form. ToExpression[input, form, h]
wraps \
the head h around the expression produced before evaluating it."

In[]:= x = 0;

Out[]= 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.)

Ok lets use a file:

In[]:=Put[OutputForm[tstring], "/tmp/test.m"];

Out[]= y^2

MLEvaluateString() is not a real ML function ?

A simple look into the C-code show, there is the infamous
ToExpression call again, together with a EvaluatePacket so
that the expression gets evaluated by the kernel.

int MLEvaluate( mlp, s)
	MLINK mlp;
	charp_ct s;
	if( MLAbort) return 0;
	return MLPutFunction( mlp, "EvaluatePacket", 1L)
		&& MLPutFunction( mlp, "ToExpression", 1L)
		&& MLPutString( mlp, s)
		&& MLEndPacket( mlp);
} /* MLEvaluate */

Evaluate string make nothing than to wait
for the answer. 

int MLEvaluateString( MLINK mlp, charp_ct s)
	int pkt;
	if( MLAbort) return 0;
	if( MLEvaluate( mlp, s)){
		while( (pkt = MLAnswer( mlp), pkt) && pkt != RETURNPKT)
			MLNewPacket( mlp);
		MLNewPacket( mlp);
	return MLError( mlp) == MLEOK;
} /* MLEvaluateString */

There is no additional dependence and it seems to be easy to copy both
You should somewehere have a MLAbort variable in your program.

It shold be simple to rewrite the functions in perl if you have access
to MLPutString() and MLPutFunction().

> Where is the optimal path thru all this to some simple environmental
> features that all other languages have???

The optimal path should be reading "The Mathematica Book" by S. Wolfram
that came with your Mathematica copy. 


  • Prev by Date: Re: CellAutoDelete
  • Next by Date: Re: When ploting a differential equation, sigularities cause big ugly vertical lines on the graph - how can I get rid of them? By some magic mathematica incantaion?
  • Previous by thread: Re: Evaluate a string
  • Next by thread: what are some good web sites for learning more about programming mathematica for a programmer who's relearning his math...