Hello ScriptingHelpers community!
Today I started to get better with string manipulation. What I want to do is:
The script takes a string, let's say "1 + 2" Script does the operation and prints it.
The code I tried:
local s = "1 + 2"
local Data = s:split(" ")
local Number
local Number2
local sum
for i,v in pairs(Data) do
if string.find(v, "%d") then
if not Number then
Number = v
print("First number is "..v)
else
print("Second number is "..v)
Number2 = v
end
elseif string.find(v, "%W") then
print("Special character is "..v)
if v == "+" then
sum = Number + Number2
elseif v == "-" then
sum = Number - Number2
elseif v == "*" then
sum = Number * Number2
elseif v == "/" then
sum = Number / Number2
end
end
end
print(sum)
So, what I did wrong? Edit: The script either finds both numbers or only first number and the special character

1 comment
Thank you so much!