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

How to test if a variable is a number or a string

Started by iliketanks1998, 11 June 2014 - 08:47 PM
iliketanks1998 #1
Posted 11 June 2014 - 10:47 PM
I am looking for a solution to test if a variable contains a number or a string, in order to return a boolean value of whether or not a variable is a number.
I am writing a program that will attempt to send a message (Variable 'a'), to recipient(Variable e), and to attempt it a specified number of times (Variable f), and need to know if the variable is numerical so that rednet.send() doesn't just throw an error and continue the program with out stopping. The program is currently in progress, and is called as a function.

completion = boolean
a, b, c, d = string
e, f, g, h = int

TrySend = function(e, a, f) – e - target, a - message, f - number of attempts
g = 0
if c <= 1 then
print("The number of send attempts must be greater than 0.")
end
while g <= f do
–in progress - to be added
g = g +1
end
return completion
end
Edited on 11 June 2014 - 09:48 PM
CometWolf #2
Posted 12 June 2014 - 12:34 AM
It really would be faster for you to just search for this…

if type(var) == "number" then
  --number
elseif type(var) == "string" then
  --string
end
You get the idea.