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

how can I change a sting to a number?

Started by THC_Butterz, 02 March 2015 - 05:42 PM
THC_Butterz #1
Posted 02 March 2015 - 06:42 PM
I am setting up a program that grabs variables from a config file, lets call it "server.cfg"

config reads like this:


floors = 5

when I load the config and print the value of floors like this:


config.open("server.cfg")
print(config.read("floors"))

I get this output:


5

my program works fine when I do this:


config.open("server.cfg")
floors = 5
if floors >= 5 then
do something
end

but when I call it in a varriable and compare it to a value like this:


config.open("server.cfg")
floors = config.read("floors")
if floors >= 5 then
do something
end

I get an error that says:


attempt to compare string with number expected, got string

what am I missing?


Edit: can this thread be closed, I got it using the tonumber function, forgot it existed untill i made this post, final code looks like this:


config.open("server.cfg")
floors = tonumber(config.read("floors"))
if floors >= 5 then
do something
end
Edited on 02 March 2015 - 05:51 PM
TheOddByte #2
Posted 02 March 2015 - 09:33 PM
Simple, use tonumber

local variable = "5"
print( type( variable ) ) --# Prints: string
variable = tonumber( variable )
print( type( variable ) ) --# Prints: number