View Full Version : how to numerically evaluate a string expression?

Hi folks, this seems like it should be simple but I haven't been able to find the answer by searching the site or the Flash 5 documentation. The problem is that:

If I go

n = Number(1+2);

then I get n = 3

but if I go

n = Number("1+2");

then I get n = NaN

Well, naturally "1+2" is not a number, it's a string. But how can I get the string to be evaluated as if it were an expression?

___________

Now, what I really want to do is to be able to have a series of variables representing numbers and operations in something like the form:

n = 1 + 2 * 3 / 4, etc.

eg. n = num1 op1 num2 op2 num3, etc.

where the num's are numbers and the op's are strings, ("+", "-", "*", "/" etc.)

The part that I have working is generating some random numbers and operations, then combining it all into one big string like this:

onebigstring = num1 + op1 + num2, etc.

The part that I don't have working is where the expression (which is now a string) needs to be evaluated out into a number. I imagine there's gotta be some simple way to do this but I've tried Number() and eval() and a couple variations of other things but I'm kinda stumped now.

Anyone out there run across this kind of issue before?

thanx.

Jesse

06-22-2001, 09:37 AM

'+' isn't a number and has no numerical equivalent. So you can't do Number("1+2"). I don't think there's anything you can do about that.

beadyen

04-09-2002, 08:45 AM

I've had a similiar problem, I'm trying to make a graphing calculator...

anyway, apparently javascript has an eval function which will evaluate a string as an expression.

Is this any help? (Haven't tried it yet, still trying to work out how to call js from within flash - yes I'm a bit of an amatuer).

someone was trying to do this exact same thing about 6 months ago... don't know if they found a solution or not, do a search, see if you can find that thread.

beadyen

04-09-2002, 10:22 PM

thanks TG will let you know if anything eventuates:)

beadyen

04-09-2002, 10:47 PM

Hello again.

Found a thread in which someone has got to the point where they can set the ._y value of a pixel-sized clip based on a mathematical function:http://www.actionscript.org/forums/showthread.php3?s=&threadid=4422&highlight=calculator

The problem is that this only works for 'hard-wired' functions - do you think its possible to dynamically set the function?

What I'm trying to do is concatenate a number or an operator on to the end of a string, say each time the user hits a button, and then have flash evaluate this as a statement.

An example:

//the "5" button:

on(Press){

_root.statementString=_root:statementString+"5";

}

//the "+" button:

on(Press){

_root.statementString=_root:statementString+"+";

}

//the "equals" button:

on(Press){

_root:statementStatement=//something like eval(_root:statementString);

}

Of course eval() in flash isn't really suited to this purpose; if anyone has a workaround or is pretty clever and has though of a completely different way of setting about this I'll be pretty stoked!

Cheers

pixelwit

04-10-2002, 07:33 AM

This might be a pretty good start:

var myString = "7.5 + 2.5*.2+ 1 ";

var currentCharacter;

var currentCharacterCode;

var tempString;

var calcArray = new Array();

//

trace ('myString = "'+myString+'"');

for (i=0; i<myString.length; i++) {

currentCharacter = myString.charAt(i);

currentCharacterCode = myString.charCodeAt(i);

// if current character is a digit

if ((currentCharacterCode<58 && currentCharacterCode>47) || currentCharacter == ".") {

tempString += currentCharacter;

// if you're at the end of the string and tempString holds a value

if (i == myString.length-1 && tempString) {

calcArray.push(tempstring-0);

delete tempString;

}

// if current character is NOT a digit

} else {

// if tempString has numbers in it add tempstring to the calc array as a number

if (tempString) {

calcArray.push(tempstring-0);

delete tempString;

}

// if current character is an operator

if (currentCharacter == "+" || currentCharacter == "-" || currentCharacter == "*" || currentCharacter == "/") {

calcArray.push(currentCharacter);

}

}

}

trace ("calcArray = "+calcArray);

trace ("");As it stands this will strip all numbers and simple operators out of a string and store them in an array.

The next step would be to run through the array and multiply the two numbers next to a "*" then modify the array to show one number where the "number", "*", "otherNumber" used to be. Repeat this for "/", "-", "+" and your array will eventually be your solution.

Coping with parentheses would probably involve using nested arrays and a recursive function.

Hope it helps,

-PiXELWiT

http://www.pixelwit.com

beadyen

04-10-2002, 08:33 AM

:D

Hi pixelwit, appreciate it!

If I understand you,

you mean that having established calcArray as a list of variables representing the numbers/operators in my string,

I'd then move through the calcArray with a loop and use if/then conditions to work out what to do with the numbers on each side of an operator, one operator at a time?

I'm guessing that in this way you could give multiplication precedence over addition for example, just like in regular statements. Cool.

Would it be worth splitting the string up into an associative array so I could easily distinguish operators from numbers (or even brackets)?

I'm learning arrays here for me so forgive me if any of these questions sound a bit daft...

Cheers,

B

pixelwit

04-10-2002, 09:01 AM

"Yes" to all the questions you asked except for the last one because I'm not exactly sure what an "associative" array is.

Just a note: I'm by no means certain that this is the best way to solve this problem, but it is the way I'd approach it.

-PiXELWiT

http://www.pixelwit.com

beadyen

04-10-2002, 09:21 AM

An associative array, according to this groovy lttle book I've borrowed (Phillip Kerman's Actionscripting in Flash)

has two variables - a name and a value for each index of the array. That way you can acces indexes (indices?) of the array by name, rather than number.:cool:

I guess it's less confusing because of this, and you could probably build a little spreadsheet type thing with it. I haven't used one before, but if it turns out to be really useful I'll pass it on.

Problem is you can't push/pull/shift it.

As far as this being 'just your approach' maybe the only problem I can see is that it's going to have to process the array for every pixel-sized mc that makes up the line graph - processor might get a bit warm? Cross that bridge when I get to it :)

thanx again

B

vBulletin® v3.8.5, Copyright ©2000-2014, Jelsoft Enterprises Ltd.