We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Evaluation of Strings as Mathematical Functions
Page Index Toggle Pages: 1
Evaluation of Strings as Mathematical Functions (Read 622 times)
Evaluation of Strings as Mathematical Functions
Oct 19th, 2007, 1:51pm
 
Hi there!
Quick question,

Say I've got the following :

Code:

String myFunction = "random(255)";
float theAnswer;


As far as I can see, there's no way for me to evaluate "myFunction" so that I can get the value into theAnswer is there ? Flash has eval() ( or It used to, I think AS3 dropped it ), I take it there's no equivilent in flash ?

So what would be the best way to tackle this ?

I'm asking because I'm coding a visualisation creation tool, and I'd like users to be able to enter in formulas to control movement/position/shape etc.
Re: Evaluation of Strings as Mathematical Function
Reply #1 - Oct 19th, 2007, 2:59pm
 
I think there is no easy way.

maybe have a look at this site.
Re: Evaluation of Strings as Mathematical Function
Reply #2 - Oct 19th, 2007, 3:38pm
 
darn.
ah well, thanks! Guess I'd better get to it!
Re: Evaluation of Strings as Mathematical Function
Reply #3 - Oct 19th, 2007, 10:29pm
 
One thing you can do is write your own mini-parser that reads the strings, figures out which functions to delegate the results to, and handles outputting the results to a float variable.

If the number of functions you're planning to allow is fairly small (basic math functions + a few other simple things), this is probably manageable, though you have to be able to figure out how to handle things like nested calls and stuff like that.

The main problem with dynamic compilation, other than the fact that it's a bit difficult, is that you wouldn't be able to deploy as an applet very easily (at the very least you'd have to sign the applet).
Re: Evaluation of Strings as Mathematical Function
Reply #4 - Oct 20th, 2007, 1:30am
 
Yeah ewjordan, That's what I decided to do last night.

So I've written one that can do basic operations ( + - / * ) understands random(number), and fills in the values for a set of variables.  Just made it so it splits a string based on spaces.  So as long as the user enters : 2 + 2 it can figure it out.

It works for now, should be enough to impress my lecturers Wink

Re: Evaluation of Strings as Mathematical Function
Reply #5 - Oct 20th, 2007, 9:57am
 
pattern matching (regular expressions) is really helpfull in such cases. processing has a new function called match() (as of 126) which does that:

Code:

String eq = "100 * 0.005";

String[] matches = match( eq, "([0-9]+(\\.[0-9]+)?)[ ]*([-+*/]{1})[ ]*([0-9]+(\\.[0-9]+)?)" );

if ( matches == null || matches.length < 5 ) {
      println( "Err" );
      return;
}

float result;
float left = float(matches[0]);
float right = float(matches[3]);

switch ( matches[2].charAt(0) ) {

   case '-':
      println( left - right );
      break;

   case '+':
      println( left + right );
      break;

   case '*':
      println( left * right );
      break;

   case '/':
      println( left / right );
      break;
}



this might be a nice example for the new (and yet undocumented) match() function ..

F
Page Index Toggle Pages: 1