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

Why can't I get a variable to equal another variable?

Started by Kansas, 01 August 2014 - 09:35 PM
Kansas #1
Posted 01 August 2014 - 11:35 PM
http://pastebin.com/MQY17TNh#

In lines 102 - 111, I'd like to get b == y to work but it just won't.

I've tried to debug it through making another script: http://pastebin.com/8JbmvhUy

..but it all comes up negative.
LeonTheMisfit #2
Posted 02 August 2014 - 12:24 AM
It looks like your problem is the way you're using variable y. The read() function returns a string and you want a number. You'll want to use Lua's tonumber() function to convert the input string to a number before using it in your loops. So you could do the following

for b = 1, tonumber(y) do
  --do stuff
end
Edited on 01 August 2014 - 10:25 PM
Kansas #3
Posted 02 August 2014 - 12:34 AM
That is exactly it! Thanks a lot, I'm still new to programming
LeonTheMisfit #4
Posted 02 August 2014 - 12:46 AM
That is exactly it! Thanks a lot, I'm still new to programming
No problem glad I could help! We were all new at some point.
Inumel #5
Posted 02 August 2014 - 05:33 AM
I know this is a bit late, but you could also do y = tonumber(read()), then just do b == y
LeonTheMisfit #6
Posted 02 August 2014 - 07:50 AM
And if you really want to make sure you're getting correct input you could do something like this

local y
repeat
  y = tonumber(read())
until y

tonumber returns nil if the given string isn't a valid number therefore this loop won't exit until a valid number is provided.
Alice #7
Posted 02 August 2014 - 08:02 AM
As has been said from other people,
repeat x until y
is a bad practice.
Instead,
local y
while true do
 term.write( "Number: " )
 y = tonumber( read() )
 if y then
  break
 end
end
theoriginalbit #8
Posted 02 August 2014 - 08:13 AM
As has been said from other people,
repeat x until y
is a bad practice.
who said it was bad practise? and why exactly did they say it was bad practice? :huh:/>
Lyqyd #9
Posted 02 August 2014 - 08:17 AM
Uh, no, if a repeat loop is the correct loop to use, use a repeat loop.
LeonTheMisfit #10
Posted 02 August 2014 - 08:19 PM
I'm curious to know who said this was a bad practice and what their reasoning was as well. From my understanding of how the repeat until loop works my example is perfectly acceptable, but I'm open to correction if a valid reason is provided. Also, in many of the languages I work with your example


local y
while true do
term.write( "Number: " )
y = tonumber( read() )
if y then
  break
end
end

would actually be considered a bad practice because it has hard to follow flow control, and while true loops are always discouraged even if a break is provided.
Edited on 02 August 2014 - 06:29 PM
Lyqyd #11
Posted 02 August 2014 - 09:29 PM
No, while true loops aren't always discouraged. Again, use the right tool for the job.
LeonTheMisfit #12
Posted 02 August 2014 - 10:23 PM
No, while true loops aren't always discouraged. Again, use the right tool for the job.

Okay to say they're always discouraged may have been the wrong way to put that. What I meant was that in many cases there is probably a better way, and one should only use it where it's necessary. Like you said, the right tool for the right job, and what I was really trying to get at is that this isn't one of the cases where it's at all the best way or necessary.
Edited on 02 August 2014 - 08:26 PM