Evaluate a string

Ok, this is probably a very simple question, but how would I evaluate a string in Corona? For example, in javascript, if I create the string “5+5” and used eval("5+5), I would get 10. I’ve been reading around and it looks like loadstring might be the right code, but I get an error msg from Corona saying loadstring is not available in corona. Any ideas? [import]uid: 19999 topic_id: 15092 reply_id: 315092[/import]

coz loadstring is available in lua, not the sandboxed lua in coronaSDK.

Would you be using a very complicated expression to evaluate? I have a small eval library written for testing that evaluates numeric expressions only, I wanted to extend it to handle the BODMAS and variables, but it was not essential to me at this point.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15092 reply_id: 55847[/import]

Have a look at this, I put that together based on your question here

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15092 reply_id: 55849[/import]

@jayantv, thanks so much for taking a stab at this. I am only looking to evaluate simple mathematical formulae, so what you demo in your video is great. I have two questions:

first, how in the world did you get a native textfield to run in the simulator? I would love that.

Second, will you make your eval code available? You’ve shown how you are using it, but not what’s under the hood. That would be very useful. As it is now, I can’t do much with it on my end. [import]uid: 19999 topic_id: 15092 reply_id: 56024[/import]

Hi,
I was out of town, I had posted that just before I left so could not respond to your reply.

  1. The video is run by building the app for the xCode simulator as there is no facility to display textboxes in the Corona simulator, I guess I heard it will be arriving soon.

  2. I can understand that unless you peek inside the bonnet or under the hood, the rest of the sample code is not very useful as that is what you have too.

I am still undecided on the direction of the code, should I open source it or put it up on donationware.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15092 reply_id: 56802[/import]

Hi, this reply is quite late… not sure how you solved the issue but I ran into this thread while having the same need…

Anyway, I have a working code, not tested yet and I might be sharing it once I feel comfortable with it.

But here is a good approach to this problem:

  1. As an input you get some string like “199 - (5 + 4) * 4”. This formation is called “infix” where the operator is between the two operands, this is how us humans write expressions :).
  2. You convert this string to the postfix form which looks like “199 5 4 + 4 * -”. In this formation the operator is placed AFTER (hence the “post”) two operand. The nice thing about this formation is that you do not need brackets and the operator precedence is embedded in the operator locations. Above all that it’s much easier to calculate in code. The algorithm for converting an infix expression to postfix is called Shunting-Yard and is available HERE.
    The only catch is there is no lua version so you’ll have to translate from some other language you are familiar with.
  3. Now the easier part. You need to write a second function that evaluates a postfix (aka RPN) expression.This sums up to reading the elements from left to right and each time you encounter an operator merge the two last elements before it (applying the operator to merge). This algorithm is also freely available HERE.
  4. Now by calling the first function and then the second function you can evaluate any human readable inflix expression. So lets say the functions are name infixToPostfix and calculateRPN you can have a third function called calculate which looks like this:
function calculate( expression )  
 local postfix = infixToPostfix( expression )  
 return calculateRPN( postfix )  
end  

hope this helps :slight_smile: it worked for me. [import]uid: 80469 topic_id: 15092 reply_id: 102413[/import]

I was looking for something similar … so I wrote one myself.  It can take a string of math and returns a number:

 

x = evalMath( "3\*(2+1)" ) print( x )   -- prints 6 

Another function, evalString, takes a table of key-value pairs and substitutes them into the string first, then calculates the result.

 

More info:  http://developer.coronalabs.com/node/38357

 

(I saw a couple of people looking for this … sorry if you see double-posting)

I was looking for something similar … so I wrote one myself.  It can take a string of math and returns a number:

 

x = evalMath( "3\*(2+1)" ) print( x )   -- prints 6 

Another function, evalString, takes a table of key-value pairs and substitutes them into the string first, then calculates the result.

 

More info:  http://developer.coronalabs.com/node/38357

 

(I saw a couple of people looking for this … sorry if you see double-posting)