Jump to content




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


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

#1 exploder

  • Members
  • 69 posts
  • LocationLatvia

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


#2 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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?

#3 exploder

  • Members
  • 69 posts
  • LocationLatvia

Posted 24 November 2012 - 05:40 AM

View PostremiX, on 24 November 2012 - 05:27 AM, said:

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?

#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 24 November 2012 - 05:42 AM

You can use tonumber() to change the type of a numeric string to a number.

#5 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 24 November 2012 - 05:48 AM

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

#6 exploder

  • Members
  • 69 posts
  • LocationLatvia

Posted 24 November 2012 - 06:25 AM

View PostLyqyd, on 24 November 2012 - 05:42 AM, said:

You can use tonumber() to change the type of a numeric string to a number.

Yes, this did the job.

View PostremiX, on 24 November 2012 - 05:48 AM, said:

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.





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users