Jump to content




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


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

#1 Kansas

  • New Members
  • 2 posts

Posted 01 August 2014 - 10: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.

#2 LeonTheMisfit

  • Members
  • 30 posts

Posted 01 August 2014 - 11:24 PM

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 by LeonTheMisfit, 01 August 2014 - 11:25 PM.


#3 Kansas

  • New Members
  • 2 posts

Posted 01 August 2014 - 11:34 PM

That is exactly it! Thanks a lot, I'm still new to programming

#4 LeonTheMisfit

  • Members
  • 30 posts

Posted 01 August 2014 - 11:46 PM

 Kansas, on 01 August 2014 - 11:34 PM, said:

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.

#5 Inumel

  • Members
  • 120 posts

Posted 02 August 2014 - 04:33 AM

I know this is a bit late, but you could also do y = tonumber(read()), then just do b == y

#6 LeonTheMisfit

  • Members
  • 30 posts

Posted 02 August 2014 - 06: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.

#7 Alice

  • Members
  • 429 posts
  • LocationBehind you.

Posted 02 August 2014 - 07: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


#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 02 August 2014 - 07:13 AM

 Alice, on 02 August 2014 - 07:02 AM, said:

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:

#9 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 02 August 2014 - 07:17 AM

Uh, no, if a repeat loop is the correct loop to use, use a repeat loop.

#10 LeonTheMisfit

  • Members
  • 30 posts

Posted 02 August 2014 - 07: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


 Alice, on 02 August 2014 - 07:02 AM, said:

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 by LeonTheMisfit, 02 August 2014 - 07:29 PM.


#11 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 02 August 2014 - 08:29 PM

No, while true loops aren't always discouraged. Again, use the right tool for the job.

#12 LeonTheMisfit

  • Members
  • 30 posts

Posted 02 August 2014 - 09:23 PM

 Lyqyd, on 02 August 2014 - 08:29 PM, said:

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 by LeonTheMisfit, 02 August 2014 - 09:26 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users