Now for the string method.
I’m not sure how familiar you are with coding, so I’ll provide a basic explanation:
A string is basically just text, and is surrounded by quotations mark (there are also alternative things to surround it with).
In
local Text = "HELLO"
the variable Text
gets assigned the value HELLO
, which is a string.
For simplificities sake, the code outlined in this post can only carry out a single operation in the text (e.g. "5 + 3"
) and not multiple, such as "2 + 5 + 7"
)
The first step is just to define the function, so:
local function Calculate(Text)
end
In terms of actually processing Text
, I would first recommend getting rid of any space characters (
), as they don’t add any information. This can be achieved using gsub
of the string
library. (gsub is basically just a replace function)
local function Calculate(Text)
local Text2, _ = string.gsub(Text, " ", "")
-- Replace all occurances of " " with ""
-- which basically just removes them
-- If Text = "5 + 3", Text2 will = "5+3"
-- Note the _ after 'Text2,'. This is basically saying to get the value obtained, but disregard it.
-- This is needed as gsub returns 2 values - the new string, plus the number of changed values.
-- As we don't care about how many were changed, we disregard it.
end
Next, I would check which operation is found within the text, using find
of the string
library. Find will return the position of the character in the string. We only care that it is found though.
local function Calculate(Text)
local Text2, _ = string.gsub(Text, " ", "")
-- Replace all occurances of " " with ""
-- which basically just removes them
-- If Text = "5 + 3", Text2 will = "5+3"
-- Note the _ after 'Text2,'. This is basically saying to get the value obtained, but disregard it.
-- This is needed as gsub returns 2 values - the new string, plus the number of changed values.
-- As we don't care about how many were changed, we disregard it.
if string.find(Text2, "+") then
-- Do the addition stuff
elseif string.find(Text2, "-") then
-- Do the subtraction stuff
elseif string.find(Text2, "*") then
-- Do the multiplication stuff
elseif string.find(Text2, "/") then
-- Do the division stuff.
else
warn("Unknown operation")
-- This outputs "Unknown operation", in orange text!
end
end
Then, we need to get the relevant numbers. We can do this using split
of the string library, which seperates the string at a specified point.
local function Calculate(Text)
local Text2, _ = string.gsub(Text, " ", "")
-- Replace all occurances of " " with ""
-- which basically just removes them
-- If Text = "5 + 3", Text2 will = "5+3"
-- Note the _ after 'Text2,'. This is basically saying to get the value obtained, but disregard it.
-- This is needed as gsub returns 2 values - the new string, plus the number of changed values.
-- As we don't care about how many were changed, we disregard it.
if string.find(Text2, "+") then
-- Do the addition stuff
local Numbers = string.split(Text2, "+")
local Num1 = Numbers[1] -- Get the first number
local Num2 = Numbers[2] -- Get the second number
elseif string.find(Text2, "-") then
-- Do the subtraction stuff
local Numbers = string.split(Text2, "-")
local Num1 = Numbers[1] -- Get the first number
local Num2 = Numbers[2] -- Get the second number
elseif string.find(Text2, "*") then
-- Do the multiplication stuff
local Numbers = string.split(Text2, "*")
local Num1 = Numbers[1] -- Get the first number
local Num2 = Numbers[2] -- Get the second number
elseif string.find(Text2, "/") then
-- Do the division stuff.
local Numbers = string.split(Text2, "/")
local Num1 = Numbers[1] -- Get the first number
local Num2 = Numbers[2] -- Get the second number
else
warn("Unknown operation")
-- This outputs "Unknown operation", in orange text!
end
end
Finally, we can do the relevent operation and return the result:
local function Calculate(Text)
local Text2, _ = string.gsub(Text, " ", "")
-- Replace all occurances of " " with ""
-- which basically just removes them
-- If Text = "5 + 3", Text2 will = "5+3"
-- Note the _ after 'Text2,'. This is basically saying to get the value obtained, but disregard it.
-- This is needed as gsub returns 2 values - the new string, plus the number of changed values.
-- As we don't care about how many were changed, we disregard it.
if string.find(Text2, "+") then
-- Do the addition stuff
local Numbers = string.split(Text2, "+")
local Num1 = Numbers[1] -- Get the first number
local Num2 = Numbers[2] -- Get the second number
return Num1 + Num2
elseif string.find(Text2, "-") then
-- Do the subtraction stuff
local Numbers = string.split(Text2, "-")
local Num1 = Numbers[1] -- Get the first number
local Num2 = Numbers[2] -- Get the second number
return Num1 - Num2
elseif string.find(Text2, "*") then
-- Do the multiplication stuff
local Numbers = string.split(Text2, "*")
local Num1 = Numbers[1] -- Get the first number
local Num2 = Numbers[2] -- Get the second number
return Num1 * Num2
elseif string.find(Text2, "/") then
-- Do the division stuff.
local Numbers = string.split(Text2, "/")
local Num1 = Numbers[1] -- Get the first number
local Num2 = Numbers[2] -- Get the second number
return Num1 / Num2
else
warn("Unknown operation")
-- This outputs "Unknown operation", in orange text!
end
end
You can then call the function like this:
Calculate("5 + 3")
Calculate("7 * 1")
Calculate("10 / 2")
You can store the result to a variable
local Result = Calculate("7 * 3")
-- Result will have the value 21
or just output it directly:
print(Calculate("10 / 2"))
-- Outputs 5
Technically, you should probably do tonumber() on Num1 and Num2
local Num1 = tonumber(Numbers[1])
local Num2 = tonumber(Numbers[2])
However it will actually work without it, as the compiler recognises that they are both numbers