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

checking what type of a variable something is

Started by Exerro, 04 January 2013 - 09:18 PM
Exerro #1
Posted 04 January 2013 - 10:18 PM
is there a way to check if a variable is a number or and boolean or something without the code crashing if it isnt?

I have tried tonumber for numbers but that would work with "3" etc and i also tried if n/4 then but that crashed the code :l

Please help :P/>
Luanub #2
Posted 04 January 2013 - 10:41 PM
use the type() function


local a = 1
print(type(a)) -- will print number
remiX #3
Posted 04 January 2013 - 10:42 PM
type()


t = { var= 3, state= true, message = "Hello, I am a string" }

for i, v in pairs(t) do
	print(i .. " is " .. v .. " which is a " .. type(v))
end
Exerro #4
Posted 04 January 2013 - 10:48 PM
wow i cant believe it is that simple *facepalm" thanks :D/>