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

[LUA] New to lua, confused about handling of numerical strings

Started by Snachmo, 20 March 2013 - 09:26 AM
Snachmo #1
Posted 20 March 2013 - 10:26 AM
Hello folks. I'm about a week in to CC and Lua, having a problem I don't understand.

I'm trying to loop through a text file of computerIDs (to send the same command to multiple machines over rednet).

Everything I've read says Lua converts numbers and strings invisibly, but when I use the result of fs.readLine() in rednet.send I get a string error.

The turtleIDs file is just one ID per line:
50
48

etc

Example:

idFile = fs.open("miningTurtleIDs","r")
while true do
  turtleID = idFile.readLine()
  if turtleID == nil then break end
  print(turtleID)
  rednet.send(turtleID,"blappity blah")
end
idFile.close()

Returns:
50
rednet:53: Expected number

If I remove the rednet.send(), it sucessfully prints the numbers.

So far I can see that string.byte will do this, but seems complicated to implement for multi-digit numbers. I have a hunch I'm missing something basic about the language here, so asking the pros :)/>/>

Any help would be great. Also I know the while true loop is poor logic, but lets call this an alpha.
Lyqyd #2
Posted 21 March 2013 - 06:25 AM
Split into new topic.

You're looking for tonumber(). Simply tonumber your numeric string and use the return value, which will be a number, or nil if the string was non-numeric.
Snachmo #3
Posted 21 March 2013 - 03:49 PM
I knew it had to be something simple. I read the lua docs for hours and didn't catch this. Facepalm, etc.

Thanks a mint, I'm off and runnin'