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

Comparing Error?

Started by MCuniverse, 03 February 2014 - 04:26 AM
MCuniverse #1
Posted 03 February 2014 - 05:26 AM
I've created a program that tests your memory span. It's not that good while it's still in development.
http://pastebin.com/2sBypZJP
The only problem is that when I see the numbers right, typing in the number identifies it as wrong. Some program modifications easily say they are the same number, but the result when checking it is "equal" is always false (or true in "not equal"). They are also pure numbers, and are not strings. Is there a reason this is happening?
surferpup #2
Posted 03 February 2014 - 05:32 AM
The problem is in the io.read() function you are using. It is reading in as a string, rather than a number, You could type convert it to a number before doing the test:

inum = tonumber(io.read())

Try that.
MCuniverse #3
Posted 03 February 2014 - 05:35 AM
The problem is in the io.read() function you are using. It is reading in as a string, rather than a number, You could type convert it to a number before doing the test:

inum = tonumber(io.read())

Try that.
Strange, it works. I wonder why? It would have did an error if you tried to compare a string with a number, and the "string" itself can be manipulated like a number…
surferpup #4
Posted 03 February 2014 - 05:38 AM
I'm just not sure if a number == string comparison will come out true. I shut down for the evening, otherwise I would test my theory.

Did you figure out if this was the problem??
MCuniverse #5
Posted 03 February 2014 - 05:42 AM
I'm just not sure if a number == string comparison will come out true. I shut down for the evening, otherwise I would test my theory.

Did you figure out if this was the problem??
I could hardly recall, but I remember coming here with a similar problem…
And no, not at all.
surferpup #6
Posted 03 February 2014 - 05:44 AM
So you are still having the problem??
surferpup #7
Posted 03 February 2014 - 05:53 AM
Okay. I just confirmed that my solution worked.

You must do a tonumber(read()) or tonumber(io.read())
Bomb Bloke #8
Posted 03 February 2014 - 05:56 AM
Under Lua, you can perform some mathematical operations on strings. Strange but true.
surferpup #9
Posted 03 February 2014 - 05:58 AM
Under Lua, you can perform some mathematical operations on strings. Strange but true.

And you can concatenate numbers. For example:


a = 4
b =67
print( a .. b )

--result 467
Edited on 03 February 2014 - 04:59 AM