Jump to content




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


  • You cannot reply to this topic
2 replies to this topic

#1 Snachmo

  • New Members
  • 2 posts

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.

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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.

#3 Snachmo

  • New Members
  • 2 posts

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'





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users