2

So, essentially i'm trying to create a function that solves for the value of x.

for example, x + 4 = 8 So I'm trying to make it so, it replaces x with "" and then it gets the symbol in front of it in this case, "+" and replaces it with "" but In Order to do so, and not remove an imaginary symbol behind it, I need to make it check if the index is 1.

My Brain Hurts.

Here's what I have for the function, I deleted some of the code for getting the index, cause It didn't work.

mathMod.solveExpression = function(variable, expression)
 if (string.find(expression, "=") == nil) then
 -- void
 else
 -- continue with search but with variable
 if (string.find(expression, variable) == nil) then
 -- void
 else
 -- ooh time for some sneaky equations
 local copy = expression
 for i = 1, #expression do
 local c = expression:sub(i,i)
 if (expression == c) then
 end
 end
 end
 end
end

/ Link To My Code`https://pastebin.com/DnKPdw2q /

Mike V.
2,22510 silver badges21 bronze badges
asked Aug 2, 2017 at 21:37

1 Answer 1

1

If your equations are all of the form var op a = b, try this code, which uses Lua pattern matching:

s=" x + 4 = 8 "
var,op,a,b = s:match("(%w+)%s*(%p)%s*(%d+)%s*=%s*(%d+)") 
print(var,op,a,b)

The pattern captures the first word as the var, skips spaces, captures a punctuation char as the operation, skips spaces, captures operand a, skips the equal sign possibly surrounded by spaces, and finally captures operand b.

answered Aug 3, 2017 at 1:32
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.