This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
fusiomax's profile picture

[Lua][Question] Help with string.match()

Started by fusiomax, 27 September 2012 - 07:45 AM
fusiomax #1
Posted 27 September 2012 - 09:45 AM
So yesterday I found out about string.match() and search patterns and to try and help familiarize myself with them I decided to make a little calculator:

Spoiler

function add(a,:D/>/>
total = a + b
print(a.." + "..b.." = "..total)
end
function subtract(a,:)/>/>
total = a - b
print(a.." - "..b.." = "..total)
end
function multiply(a,B)/>/>
total = a * b
print(a.." * "..b.." = "..total)
end
function divide(a,B)/>/>
total = a / b
print(a.." / "..b.." = "..total)
end
local total
local run = 0
while run ~= 1 do

term.clear()
term.setCursorPos(1,1)

term.write("Please enter calculation: ")
input = read()
if input == "exit" then
  run = 1
  break
end
termOne, math, termTwo = string.match(userInput,"(%d+)%s*(%p?)%s*(%d+)")
termOne = tonumber(termOne)
termTwo = tonumber(termTwo)

if math == "+" then
  add(termOne, termTwo)
elseif math == "-" then
  subtract(termOne, termTwo)
elseif math == "*" then
  multiply(termOne, termTwo)
elseif math == "/" then
  divide(termOne, termTwo)
else
  print("MATH ERROR.")
end
end

So it runs but when I try to do some math I get an error "calc:34: bad argument : string expected, got nil"

Now I would assume that this means that in my sting.match() function I used a search argument in there, and I would think that this was the (%p?), in case you're not sure that is to find the +,-,*,/ to determine what the user wants to do.

Any help would be appreciated muchly :P/>/>
MrBarry #2
Posted 27 September 2012 - 10:02 AM
Variable names are case sensitive. b is not B. What happens when you change your function parameters from (a, B) to (a, b)
fusiomax #3
Posted 27 September 2012 - 10:14 AM
Variable names are case sensitive. b is not B. What happens when you change your function parameters from (a, :P/>/> to (a, :D/>/>

That's strange, in the code on my PC those capital B's are lower case.

Here's a repaste :
Spoiler

function add(a, :)/>/>
total = a + b
print(a.." + "..b.." = "..total)
end
function subtract(a, B)/>/>
total = a - b
print(a.." - "..b.." = "..total)
end
function multiply(a, B)/>/>
total = a * b
print(a.." * "..b.." = "..total)
end
function divide(a, B)/>/>
total = a / b
print(a.." / "..b.." = "..total)
end
local total
local run = 0
while run ~= 1 do

term.clear()
term.setCursorPos(1,1)

term.write("Please enter calculation: ")
input = read()
if input == "exit" then
  run = 1
  break
end
termOne, math, termTwo = string.match(userInput,"(%d+)%s*(%p?)%s*(%d+)")
termOne = tonumber(termOne)
termTwo = tonumber(termTwo)

if math == "+" then
  add(termOne, termTwo)
elseif math == "-" then
  subtract(termOne, termTwo)
elseif math == "*" then
  multiply(termOne, termTwo)
elseif math == "/" then
  divide(termOne, termTwo)
else
  print("MATH ERROR.")
end
end

it keeps capitalizing those b's when I paste it here.
fusiomax #4
Posted 27 September 2012 - 02:26 PM
I figured it out, I called the variable userInput instead of input (derp) for anyone interested here's the code:
Spoiler

function add(a,:P/>/>
total = a + b
print(a.." + "..b.." = "..total)
sleep(1)
end
function subtract(a,:D/>/>
total = a - b
print(a.." - "..b.." = "..total)
sleep(1)
end
function multiply(a,:)/>/>
total = a * b
print(a.." * "..b.." = "..total)
sleep(1)
end
function divide(a,B)/>/>
total = a / b
print(a.." / "..b.." = "..total)
sleep(1)
end
local total
local run = 0
while run ~= 1 do

term.clear()
term.setCursorPos(1,1)

term.write("Please enter calculation: ")
input = read()
if input == "exit" then
  run = 1
  break
end
termOne, math, termTwo = string.match(input,"(%d+)%s*(%p?)%s*(%d+)")
termOne = tonumber(termOne)
termTwo = tonumber(termTwo)

if math == "+" then
  add(termOne, termTwo)
elseif math == "-" then
  subtract(termOne, termTwo)
elseif math == "*" then
  multiply(termOne, termTwo)
elseif math == "/" then
  divide(termOne, termTwo)
else
  print("MATH ERROR.")
end
end