767 posts
  • Has been part of the Envato Community for over 5 years
  • Has sold $1,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Sells items exclusively on Envato Market
+2 more
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

1680 posts Envato Elite Author
  • Has referred 10+ members
  • Has sold $125,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+6 more
Tean says

I dont get what are you tring to do…

why do you need strings if you work with numbers?

767 posts
  • Has been part of the Envato Community for over 5 years
  • Has sold $1,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Sells items exclusively on Envato Market
+2 more
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.

1680 posts Envato Elite Author
  • Has referred 10+ members
  • Has sold $125,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+6 more
Tean says

Are you talking about casting?

767 posts
  • Has been part of the Envato Community for over 5 years
  • Has sold $1,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Sells items exclusively on Envato Market
+2 more
outandouter says

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

1680 posts Envato Elite Author
  • Has referred 10+ members
  • Has sold $125,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+6 more
Tean says

So split them by empty spaces and cast each as number

20 posts
  • Has been part of the Envato Community for over 3 years
  • Has sold $100+ on Envato Market
  • Located in Canada
  • Has collected 1+ items on Envato Market
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

3787 posts Community Moderator
  • Helps us moderate the forums
  • Contributed a tutorial on Tuts+
  • Located in Netherlands
  • Made it to the Authors' Hall of Fame
+9 more
Joost 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
  • Has been part of the Envato Community for over 5 years
  • Has sold $1,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Sells items exclusively on Envato Market
+2 more
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!

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by