yea that wouldn’t work sorry
“1 + 1” would print out nil because you can’t turn that into a number
also Print() isn’t a thing
yea that wouldn’t work sorry
“1 + 1” would print out nil because you can’t turn that into a number
also Print() isn’t a thing
Oh ok. I’m not a good scripter so I would not know.
that does not work is the same
local var = "1 + 1"
print(tonumber(var))
=
print(tonumber("1 + 1"))
when will they answer me???
Hang in there. I’m working on finding a solution right now.
Try this
local var = “1” + “1”
print(tonumber(var))
wait one second I already said this
are you only doing math related things?
do you want a string calculator?
I’m not very good at string manipulation. There are many ways to go about this, but this is just my simple way at achieving what you want.
local test = "5 + 5"
local operations = {
["+"] = function(a, b)
return a + b
end,
["-"] = function(a, b)
return a - b
end,
["*"] = function(a, b)
return a * b
end,
["/"] = function(a, b)
return a / b
end,
}
local values = string.split(test, " ")
local x = values[1]
local oper = values[2]
local y = values[3]
print(operations[oper](x, y))
yes that’s what my real code was about
my system only allows full strings
If you’re heart-set on using a string to denote your mathematical expression, you could always do this:
local expr = '1 + 1'
local eval = loadstring(string.format('return %s', expr))()
print(eval) --> 2
Beware though, using this in combination with user input will open an opportunity for code injection
Because your using “” and trying to print a function (math in this case) - print(tonumber(1+1))
EDIT: you don’t have to use tonumber for this
You can probably just override the environment of the created function to prevent code injection
local func = loadstring(string.format('return %s', expr))
setfenv(func, {math = math})
local eval = func()
print(eval)
The issue is that the string is not a number.
Computers do not understand context, so to a computer, the string “1 + 1” literally means “one space plus space one”.
tonumber only works on strings that contain ONLY numbers.
better give me a solution to my problem
function StringCalculator(String)
local operations = {
["+"] = function(a, b)
return a + b
end,
["-"] = function(a, b)
return a - b
end,
["*"] = function(a, b)
return a * b
end,
["/"] = function(a, b)
return a / b
end,
}
local values = string.split(test, " ")
local x = values[1]
local oper = values[2]
local y = values[3]
return operations[oper](x, y)
end
StringCalculator("1 + 1")
I made @itsLevande’s code into a function so it is better then it was
sadly this only works for two numbers and only one operator and you need spaces to separate the numbers and operator, this is a very very basic string calculator.
loadstring() or another code exucuter is probably better then this
also sorry this took me forever, train wifi is horrible
There’s a way to expand the capability to a string of any length with any number of operators. The Shunting-Yard algorithm is one that can parse string expressions with no risk of code injection.
While I would love to implement this algorithm in lua, I don’t want to.
I realize this, I was mainly talking about the code you gave and what limitations it had
I could probably make this if you wanted me to, I mean not anytime soon because I’m working on a game but when I start working on my math module again I could try and implement this into it
Good idea. I also had the idea to incorporate string pattern matching as well. Here is the result of me combining that idea with yours:
local function parse(expr)
local strcmp = ''
for str in string.gmatch(expr, '[%(%)]*%d-[%.%d-]*%s*[%+%-%*/%^]*%s*') do
strcmp = strcmp .. str
end
return strcmp ~= expr and 0 or setfenv(loadstring(string.format('return %s;', expr)), { })();
end
local badmath = '1.3 + 4 and print("a")'
print(badmath,'=',parse(badmath)) --> 0
local goodmath = '(1.3+4.6)^ 2 - 1'
print(goodmath,'=',parse(goodmath)) --> 33.81
Though OP would likely get the best benefit from learning about the algorithm that @itsLevande linked