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

For Variable and user inputed variable not comparing. Lua

Started by 8nightwing, 06 January 2013 - 02:19 AM
8nightwing #1
Posted 06 January 2013 - 03:19 AM
I'm trying to get == to return true when a user inputed value is compared to the "i" variable in a for loop.

I've made a test program here http://pastebin.com/snB9iNsz that correctly compares "i" and "torches" with == like it should outputing this.

1 3
2 3
3 3
this actually prints!
4 6
5 6
6 6
this actually prints!
7 9
8 9
9 9
this actually prints!
10 12

However, when I have the program http://pastebin.com/630F7vei ask the user for the value of "torches" the == doesn't reconize "i" and "torches" as being the same number.

1 3
2 3
3 3
4 3
5 3
6 3
7 3
8 3
9 3
10 3

I'm just now starting to learn lua but have some c++ experience. I noticed that in lua you don't have to declare the variable type like you do when creating variables in c++. That has lead me to believe that the issue is lua not assigning the inputed variable as an int but as a char and that is why == is not thinking 3 and 3 are the same.

Now that I said that I guess my "real" question would be is it possible to declare the type of a variable in lua? and if not is there a work around possible that will achieve the result I'm looking for?

Thanks for the help!
ChunLing #2
Posted 06 January 2013 - 03:27 AM
There are only a few types in Lua, number (floats, but they can emulate most other numeric types with little trouble for most purposes), string, boolean, function, and table (and nil, which I don't think of as a real type so much as no type).

All data collected with read() is going to be string. You can convert it to number by using tonumber(), so " var = tonumber("15") " will leave var with a number value of 15. Many arithmetic operations in Lua will automatically tonumber a string, but equality comparison does not.
8nightwing #3
Posted 06 January 2013 - 04:05 AM
Thank you so much using tonumber() fixed the issue completely! I've been trying to find a list of all lua commands in computercraft but for the life of me I can't find one. Thanks again though that worked perfectly :)/>
ChunLing #4
Posted 06 January 2013 - 04:19 AM
Go to the source of all Lua. Or at least of 5.1 Lua (we're still using that, right?).