This content has been marked as final. Show 5 replies
I'm not sure I understand your exact requirements.
Do you want to evaluate the string (calculate its value) based on the actual values of the variables?
For that you could use String methods to replace the text names of the variables with their acual values
And then use a some 3rd party expression parsing code
thank you for your reply,
I would like the string to be used as an actionscriptline in Flash; so eg...if the input of a user would be: x*x+dy*dy
then the as code would be:
so if x=3 and dy = 0.1 the value of aa would be 9.06.
But if a user should input x+dy then the code would be:
and the value of aa would be 3.01.
So the user is writing the programming line after aa= ;
Splitting the string is no problem and with eval("x") I can change the string into a variable, but the problem is the operator * since eval("*") doesn't work...
Ok, so I think my advice stands then...
You could write your own expression parser, using eval("x") when you came to parse an x in the string etc...and its probably not too complicated to do if you're just dealing with multiplication and addition.... or you could use existing code like the example in the link I posted earlier to parse the expression (I've used that code before for something more complex).
If you were writing your own parser you could use eval("x") ...but if it were me... I would do the opposite. That is to split and join the string with the variable names, subsitituting their values into the string and then run it through that expression parser. Its basically just calculating a string version of a numeric expression then with no variables.
For your variable --> value substitution (split and then join) you need to do it in the order of the
dx and dy
then x and y
so you get it to work correctly.
ok, thnx, i will follow your advice
You're welcome. Its probably the easy/lazy way out in terms of advice...
That code is probably overkill if its only ever going to be addition and subtraction. But if there's the potential for your expressions to have (for example) trig functions or more complicated operators then it saves you writing it.