Running a string

I have a string some like "2x + 5 ^ 2 * (3.5 + x)", so I already searched for it, and if I just knew what words I shall type in, etc. I already have replace X to an integer. How to calculate a string???

@TokOut use loadstring.
E.g if you have thing = "(2x+5)^2 * (3.5+x)" (I think you need brackets around the 2x+5) then you can do:

x = 3
loadstring("value="..thing)()
print(value)

And also, one “?” should be enough.

@MattthewLXXIII Ignore my last post to you.

@dave1707 to me or TokOut?

I guess it can be to both of you. When I replied, I ignored that the calculation was a string and that your loadstring was the correct answer. @TokOut I’m not sure why you even asked this question. Back in December, you wrote some code that used the loadstring command. You had to know what its purpose was then and how it was used to be able to use it in your code. I think you ask questions just so you don’t have to waste time looking for an answer.

@MattthewLXXIII @TokOut You need a * between the 2x i.e. 2 * x.

function setup()
    thing = "2 * x + 5 ^ 2 * ( 3.5 + x )" 
    x = 3
    loadstring("value="..thing)()
    print(value)
end

I am hearing of Loadstring for the first time ;( @dave1707. And I searched for “run”, “running string”, all I received was 5 ads

Does loadstring()() actually exists?

GraphicsGet:60: attempt to call a nil value
stack traceback:
	GraphicsGet:60: in method 'diagramTable'
	GraphicsGet:36: in method 'draw'
	Main:13: in function 'draw'

There I had my Loadstring function

Um, @TokOut, you have seen loadstring before - this is you writing in December last year

Here is my calculator in 4 lines format:

function draw()
http.request("https://raw.githubusercontent.com/TokOut/InOneClass/master/CalculatorReleased.lua",function(d) loadstring(d)() setup() end,function(e) print(e)end) 
end

Did you notice the loadstring in there?

See it for yourself
https://codea.io/talk/discussion/7086/calculator-v1-0-project-released-now-2-0

Re your question about loadstring()() - try actually READING some explanations of loadstring on the internet, before you ask. We don’t have the time to teach you things you can find for yourself

I copied that Code from a rocket ship, and putted there my link. I didn’t saw the function that times :stuck_out_tongue:

@TokOut This is what you said then: Here is my calculator in 4 lines format:'. You didn’t say that you copied it when you posted it. It would be helpful to us if you say that you copy code or you write it yourself. We help users based on how well we think they know how to write code. When you post large programs that work without errors, we assume you know what your doing and when you ask simple questions, we assume you’re just being lazy.

Okay, this is how I used loadstring:

    -- Create Table
    if not self.editing then
        local s = 40
        for r = 0, s * 2 do
            r = r - s
            local a = self.input
            a = a:gsub("x", r)
            if not a then
                a = "x"
            end
            loadstring("c = " .. a)()
            table.insert(t, 1, vec2(x + r * self.size, y + c * self.size))
        end
        
        -- Dots from Table
        strokeWidth(0)
        fill(0, 65, 255, 255)
        clip(285, 150, 680, 600)
        for a, b in ipairs(t) do
            ellipse(b.x, b.y, 15)
        end
        
        -- Connect dots to graphic
        strokeWidth(2)
        fill(0, 65, 255, 255)
        for a, b in ipairs(t) do
            if not (a == #t) then
                line(b.x, b.y, t[a+1].x, t[a+1].y)
            end
        end
    end
    clip()

Question: If my Loadstring is wrong: “2x +” - not finished, it stops the project with this message:

GraphicsGet:51: attempt to call a nil value
stack traceback:
	GraphicsGet:51: in method 'draw'
	Main:13: in function 'draw'

Line 51 is the Loadstring function

How should I trade with Loadstring errors?

Check the Lua docs for pcall it stands for protected call.