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

[Lua] Checking if it's a string

Started by mrSLIMEguy, 23 September 2012 - 01:28 AM
mrSLIMEguy #1
Posted 23 September 2012 - 03:28 AM
See, if I want to make a program like this:

Enter a number and I will tell you if it's higher than five.

Number:

And I do this code:


print ("Enter thy number and I will tell you if it is higher than five")
print ("")
write "Number:"
number = read()
if number > 5 then
print ("Thy number ".. number.." is larger than 5")
else
print ("Thy number ".. number.."is smaller than 5")
end


Firstly, does an integer have to be converted to go in print?
Ex:

number=5
print("Number is ".. number.." Hooray!")

Finally, how do I make sure the program doen't crash if I put a string into "number = read()"
GopherAtl #2
Posted 23 September 2012 - 04:09 AM
read always gives strings, even if they typed numbers, so convert with tonumber(number) after number=read(). tonumber() returns nil if it couldn't convert, so you can easily check with an if to confirm it was a valid number. Example:


local number
while true do
  --prompt
  term.write("Gimme a number : ")
  --read input
  number=read()
  --convert to a number
  number=tonumber(number)
  --if it isnt nil
  if number then
	break --break out of while true loop
  end
  --complain
  print("that...was NOT a number!")
--end loop, repeating
end

--your comparison here
if number>5 etc...
mrSLIMEguy #3
Posted 23 September 2012 - 05:27 AM
read always gives strings, even if they typed numbers, so convert with tonumber(number) after number=read(). tonumber() returns nil if it couldn't convert, so you can easily check with an if to confirm it was a valid number. Example:


local number
while true do
  --prompt
  term.write("Gimme a number : ")
  --read input
  number=read()
  --convert to a number
  number=tonumber(number)
  --if it isnt nil
  if number then
	break --break out of while true loop
  end
  --complain
  print("that...was NOT a number!")
--end loop, repeating
end

--your comparison here
if number>5 etc...
umm i dont understand how to do that…
Grim Reaper #4
Posted 23 September 2012 - 07:08 AM
A simpler means of attacking this problem is to use lua's built in 'type()' method.

type() takes 1 parameter, which is the variable that you're checking the type of.
For example, if I wrote the following script:

local nNumber = 1
local sString = "Test"

print( type( nNumber ) )
print( type( sString ) )
The output would look like this:

number
string

Numbers don't have to be converted to any different types. Unlike many compiled languages, lua (an interpreted language) handles all type conversions for you, if they are reasonable.
The following numbers all have type "number":

1, 2.8, -12.483298754, 2.343e17

read() returns a string type so, as Gopher suggested, you should force it the type "number".

x = tonumber( read() )
However, if you get a string then you could potentially cause logic errors. Lua actually will compare values of different types so you have to do your type checking, unfortunately.

Here is a revised version of your script that will check for string types and handle those problems:

print ("Enter thy number and I will tell you if it is higher than five")
print ("")

write "Number:"
-- Try to force the type of number onto whatever value is returned by read().
number = tonumber( read() )

-- If the type of the value held by number was successfully casted to a number,
-- therefore having the type of "number".
if type( number ) == "number" then
  if number > 5 then
    print ("Thy number ".. number.." is larger than 5")
  else
    print ("Thy number ".. number.."is smaller than 5")
  end
  -- If the type of the value beld by number is not "number" then throw an error.
else
  error( "Value entered is not a number." )
end

Hope I helped! :P/>/>
GopherAtl #5
Posted 23 September 2012 - 07:22 AM
:scratch:
mrSLIMEguy #6
Posted 30 September 2012 - 04:47 AM
A simpler means of attacking this problem is to use lua's built in 'type()' method.

type() takes 1 parameter, which is the variable that you're checking the type of.
For example, if I wrote the following script:

local nNumber = 1
local sString = "Test"

print( type( nNumber ) )
print( type( sString ) )
The output would look like this:

number
string

Numbers don't have to be converted to any different types. Unlike many compiled languages, lua (an interpreted language) handles all type conversions for you, if they are reasonable.
The following numbers all have type "number":

1, 2.8, -12.483298754, 2.343e17

read() returns a string type so, as Gopher suggested, you should force it the type "number".

x = tonumber( read() )
However, if you get a string then you could potentially cause logic errors. Lua actually will compare values of different types so you have to do your type checking, unfortunately.

Here is a revised version of your script that will check for string types and handle those problems:

print ("Enter thy number and I will tell you if it is higher than five")
print ("")

write "Number:"
-- Try to force the type of number onto whatever value is returned by read().
number = tonumber( read() )

-- If the type of the value held by number was successfully casted to a number,
-- therefore having the type of "number".
if type( number ) == "number" then
  if number > 5 then
	print ("Thy number ".. number.." is larger than 5")
  else
	print ("Thy number ".. number.."is smaller than 5")
  end
  -- If the type of the value beld by number is not "number" then throw an error.
else
  error( "Value entered is not a number." )
end

Hope I helped! :)/>/>

Thanks, this helped alot.
But what does error() do?
I cant find it in the wiki..
Does it print a message and quit the program?
Kingdaro #7
Posted 30 September 2012 - 05:04 AM
Pretty sure all error() does is quit the program with the specified error message.