## Is it possible to allow the user to calculate in my textfield?

Currently I have a textField that the user enters a value into, for example "100". Then this number is then placed into a formula and a new value is made. Is is possible to allow the user to enter "100 + 50" into the textField and then the value 150 is sent into the formula?

### Replies

Sorry I didn't even think about specifying. It would only have to be very simple because I only need addition and subtraction to work. I'm assuming this is the "operand1 operator operand2" system you were talking about but I'm not too sure I understand what that means.

When I was experimenting I got "attempt to perform arithmetic on a nil value". So what would my first step be in separating the "1" from the "+" from the "1" for instance?

Operator is a term describing addition, subtraction, multiplication, etc. Math operators take two "operands" for the calculation.

Look up string.match() and learn about using patterns.

Okay thanks! So if the user enters "10 + 15" into my textField, am I on the right track here?

newValue = strings.match (textFieldA.text, "%d+ %x %d+" )

Here I would somehow have them add or subtract and then I can have newValue plugged into my main formula?

And what if the user enters "10 + 15+ 20"

Sorry for all the questions, its just that most examples only show how to print patterns they find.

I'm not much of a string expert, but you might try something like finding all of the plus or minus symbols and getting the number(s) in between and going from there. I think I've done something like that when I was trying to figure out how to solve linear equations with code.

Something like this:

1. Delete all unneeded characters (I'd start with converting it all into a table first and going through the table to check if the characters aren't needed, things like ":,@!#%^&*()[]" etc)

2. Find all of the operators you need to

3. Get the values between them (perhaps using a loop - find operator 1 and then loop through to operator 2 and get the values in between)

4. tonumber() them

5. Perform the necessary math according to the operators

C

PS: I never really figured out how to do linear equations with code :)

Thanks! So to make it really simple on myself let's say the user can only add numbers in the textField. For example "10 + 15 +20". How would I go about converting it into a table to calculate a value and then clearing the table for the next time the user inputs data?

What I'm thinking is that I only care about numbers entered, so:

newValue = strings.match (textFieldA.text, "%d+" )

(I don't know how to tell it to search for all numbers in case there's let's say 3 or even 4 numbers entered)

And then I need the numbers found to be sent to a table.

local myTable = {}

myTable[1] =

myTable[2] =

And then at this point I would somehow have each numerical value the user entered to self assign to the table (not sure on how) and then I just tonumber() and add them up.

I'm new to tables as well so this is what I referred to.

http://www.coronalabs.com/blog/2011/06/21/understanding-lua-tables-in-corona-sdk/

Using string.match there are a couple of things to know. Use parens to capture values:

leftOperand, operator, rightOperand = string:match("10 + 20", "(%d+)(%x)(%d+)")

would capture the first number of any length > 0 (at least 1 digit) and put it into leftOperand. It would then capture the first symbol that it sees and put it into the variable operator, then finally capture another number and put it in rightOperand.

This of course doesn't handle any white space or multiple numbers. If you're going to allow more complex formula entry like 10 + 20 + 30, then you're going to probably have to come up with a more complex parsing system

You might start with these helper functions to convert strings to tables and remove any characters that aren't numbers/operators:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | local cleanChars=" \"\'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_{}|~/*=<>," local function stringToTable(str) local t={} for i=1, #str do t[i]=str:sub(i, i) end return t end local function cleanTable(t) local newTable={} for i=1, #t do if not cleanChars:find(t[i]) then newTable[#newTable+1]=t[i] end end return newTable end |

C

You would have to parse the string and perform the math requested, which isn't a trivial task, but on the other hand it's not that hard either. It all depends on how complex you want to allow: () grouping? multiple operators? 10 * 30 + 72 - (64 / 12) or just a simple "operand1 operator operand2" system.

The recent time tutorial on our blog shows how to parse a string for numbers and symbols and can put you on the way to parsing the string.