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

[Lua]How to extract value/number out of table?

Started by exploder, 24 November 2012 - 04:22 AM
exploder #1
Posted 24 November 2012 - 05:22 AM
Hello pros I have a little problem I can't solve. I have tried this in many ways but still can't get it working.

At start inside text file there is one number 0 and nothing else, but the turtle part never thinks that there is number 0 so when I start the program turtle just keeps flying up in the sky, but it should read function start() because the number inside the file is 0. I guess there is some kind of mistake I have made.

Btw, if I just type print(""..cord) then it will return 0 (or whatever number is inside text file) so I can't understand why print works but if doesn't.

Spoiler

function check()
   file = fs.open("Data/down","r")
   data = {}
   line = file.readLine()
   table.insert(data,line)
   file.close()
		cord = data[1]
		 if cord == 0 then start() else [b]--{Here is the part where he should run start() instead of the code below}--[/b]
		  repeat [b]--{Here is the part which is looping and number never reaches 0}--[/b]
			turtle.digUp()
			turtle.up()
			 cord = cord-1
			 until cord == 0
end
   end
remiX #2
Posted 24 November 2012 - 05:27 AM
Could it be because when you read it from a file, it's a string and not a number?
exploder #3
Posted 24 November 2012 - 05:40 AM
Could it be because when you read it from a file, it's a string and not a number?

Well the print command returns 0 so I guess it's a number, but if it's a string then how could I make it a number so if could read it as number?
Lyqyd #4
Posted 24 November 2012 - 05:42 AM
You can use tonumber() to change the type of a numeric string to a number.
remiX #5
Posted 24 November 2012 - 05:48 AM
Make it print(type(cord)) to check if it is a string/number
exploder #6
Posted 24 November 2012 - 06:25 AM
You can use tonumber() to change the type of a numeric string to a number.

Yes, this did the job.

Make it print(type(cord)) to check if it is a string/number

It returned string so it was a numeric string, but tonumber() made it into number and now it's working correctly.


Thank you both.