0x59
0x59

Reputation: 87

How would I get the index of a char in a string?

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 /

Upvotes: 2

Views: 474

Answers (1)

lhf
lhf

Reputation: 72412

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.

Upvotes: 1

Related Questions