767 posts
  • Author had a Free File of the Month
  • Bought between 50 and 99 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Sold between 1 000 and 5 000 dollars
outandouter says

Creative human-race,

var pi:Number = Math.PI;
var radius:Number = 6;

var expression1:String = "pi*(radius*radius)";
var expression2:String = "radius*2";

var result1:Number = ?;
var result2:Number = ?;

Have a look at the above few lines of code. I have a couple of Numbers first. Then, I have two Strings. Each String represents a mathematical expression. As clearly evident, if I remove the inverted commas from these expressions and set the variable data type as Number, I will get the desired result.

My question is, that how can I achieve the same result without taking the expression out of inverted comas? Is there any way?

- Kamil Waheed

1675 posts Envato Elite Author
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Author had a Free File of the Month
  • Exclusive Author
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Europe
  • Has been a member for 4-5 years
  • Referred between 10 and 49 users
+1 more
Tean says

I dont get what are you tring to do…

why do you need strings if you work with numbers?

767 posts
  • Author had a Free File of the Month
  • Bought between 50 and 99 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Sold between 1 000 and 5 000 dollars
outandouter says

@Tean What I want to achieve is to have the user enter the formula they want in an input box, and then from the string of that formula, deduce the numerical result.

Like if they enter, “radius*2 + 5 * pi” inside the input box, then according to the above given condition, the code should be able compute the result as 19.73.

1675 posts Envato Elite Author
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Author had a Free File of the Month
  • Exclusive Author
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Europe
  • Has been a member for 4-5 years
  • Referred between 10 and 49 users
+1 more
Tean says

Are you talking about casting?

767 posts
  • Author had a Free File of the Month
  • Bought between 50 and 99 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Sold between 1 000 and 5 000 dollars
outandouter says

Yes, that’s right. But casting a whole expression rather a single data type.

1675 posts Envato Elite Author
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Author had a Free File of the Month
  • Exclusive Author
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Europe
  • Has been a member for 4-5 years
  • Referred between 10 and 49 users
+1 more
Tean says

So split them by empty spaces and cast each as number

20 posts
  • Bought between 1 and 9 items
  • Canada
  • Has been a member for 3-4 years
  • Sold between 100 and 1 000 dollars
tjp says

You’ll have to cast each item seperately. You could create a function that splits your expressions by using delimiters (eg: // ), but either way you will probably need to use multiple functions.

public function MainFunc(str:String):Number { return GetExp("exp 1") + GetExp("exp 2"); }
private function GetExp(exp:String):Number { return Number(exp); }

Obviously you will have to change your functions and values accordingly, but this is one method you could use.

I hope it helps.

Ty

3670 posts Community Moderator
  • Has been a member for 5-6 years
  • Contributed a Tutorial to a Tuts+ Site
  • Netherlands
  • Community Moderator
  • Envato Studio (Microlancer) Beta Tester
  • Sold between 10 000 and 50 000 dollars
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Exclusive Author
+4 more
Joost Volunteer moderator says
Tean said
So split them by empty spaces and cast each as number

This. You could use separators like the plus-sign and the multiplication-asterisk to split them. Make sure to use the proper order of computation (i.e. first splitting on multiples and then later on plus signs). Mind you, things like brackets (especially when they’re nested) could become quite tricky. Perhaps regex would help.

EDIT : If it’s AS2 , you could just use eval() and that would do exactly what you’re looking for ;) As AS3 does not have this function, you might even want to consider passing it to an external Javascript frame or PHP page (eww!). Parsing it as a grammar would definitely be a more elegant solution, though!

EDIT2 : This dude seems to have gone all the way there! Seems pretty awesome! :)

EDIT3 : A guy in the same thread claims that
var val:Number = Eval("9+6+2*3-12/12+3*7-4/2");
trace(val);
function Eval(str:String):Number{
    return ExternalInterface.call("function(){return eval(\""+str+"\");}");
}
does the trick – I guess that’s the Javascript implementation right there. I don’t have a compiler near me right now so I can’t try, but you have a swing at that. Should work!
767 posts
  • Author had a Free File of the Month
  • Bought between 50 and 99 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Sold between 1 000 and 5 000 dollars
outandouter says
Joost said
Tean said
So split them by empty spaces and cast each as number

This. You could use separators like the plus-sign and the multiplication-asterisk to split them. Make sure to use the proper order of computation (i.e. first splitting on multiples and then later on plus signs). Mind you, things like brackets (especially when they’re nested) could become quite tricky. Perhaps regex would help.

EDIT : If it’s AS2 , you could just use eval() and that would do exactly what you’re looking for ;) As AS3 does not have this function, you might even want to consider passing it to an external Javascript frame or PHP page (eww!). Parsing it as a grammar would definitely be a more elegant solution, though!

EDIT2 : This dude seems to have gone all the way there! Seems pretty awesome! :)

EDIT3 : A guy in the same thread claims that
var val:Number = Eval("9+6+2*3-12/12+3*7-4/2");
trace(val);
function Eval(str:String):Number{
    return ExternalInterface.call("function(){return eval(\""+str+"\");}");
}
does the trick – I guess that’s the Javascript implementation right there. I don’t have a compiler near me right now so I can’t try, but you have a swing at that. Should work!

Thanks alot Joost. I am really impressed by the solution on EDIT3 . As for the other two, I’m not quite sure and yes, I’m using AS3 .

Actually, I HAVE found the solution. There’s this guy at kirupa forums who has written a very neat little class to solve the problem. It works perfectly for me. I would drop the link for everybody else to see:

http://www.kirupa.com/forum/showthread.php?251592-Equation-Parser

@tfp @Tean Thanks guys!

by
by
by
by
by
by