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

Number expected, got string.

Started by Specter Twilight, 01 April 2014 - 11:13 AM
Specter Twilight #1
Posted 01 April 2014 - 01:13 PM
I am currently attempting to rewrite my original Quarry preparation program (A program that would have the turtle dig out a 32x5x32 area for a Buildcraft Quarry to prevent those blocks in that area from being destroyed by the Quarry's setup) to be more versatile and efficient.

While the code is nowhere close to being finished, I've already encountered an error. As I am fairly new to Lua, I expect it to merely be my lack of knowledge on Lua's logic.

http://pastebin.com/YVCLuYU7

Whenever running the code, the error provided, while I am unable to get into the game and quote it directly, states that on line 3, 'Attempted to compare string with number, got string.' I'm fairly certain this means that it attempted to compare "read()" against "> 0" and "<= 64", but failed as the string "read()" is, indeed, a string and not a number. However, entering in a number in for line 1's write string will produce the same result. As will assigning read() to a variable and entering the variable instead, which can be seen in the code as of now.

To reiterate, I'm fairly certain this is little more than many own lack of knowledge on Lua's logic. I would appreciate if someone could inform me on the proper way to format this code so it identifies "read()" as what the user input and not as a string.
CometWolf #2
Posted 01 April 2014 - 04:05 PM
read will always return a string, if you want a number however use

n = tonumber(read())
tonumber will convert the given string to a number type. Keep in mind that if the given string is more than numbers and math symbols, it will fail and return nil.
Specter Twilight #3
Posted 02 April 2014 - 12:07 AM
read will always return a string, if you want a number however use

n = tonumber(read())
tonumber will convert the given string to a number type. Keep in mind that if the given string is more than numbers and math symbols, it will fail and return nil.

You have my gratitude. The comparisons now function as intended.