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

[Question]Is it possible to do this in lua?

Started by Papsern, 20 November 2012 - 02:58 PM
Papsern #1
Posted 20 November 2012 - 03:58 PM
In a program I'm making I need to be able to do something like this:

for i = 1,5 do
   print("1") or print("2")
end

This would print:
1
2
1
2
1

I know this code doesn't work and I wouldn't be using it to print numbers anyway, but I need to know if there is any way to do this.

Thanks in advance.
ChunLing #2
Posted 20 November 2012 - 04:06 PM
for i = 2,6 do print(math.fmod(i,2)+1) end
Kingdaro #3
Posted 20 November 2012 - 04:25 PM
I would personally prefer to use the % operator over math.fmod, it's preference though. Just throwing another option out there.

for i = 2,6 do print(i%2 + 1) end

Also, I feel that what's going on here should be explained. "%" or "fmod" is an operator that returns the remainder of the division of the two numbers given.

In that, 7/2 is 3 with a remainder of 1, so 7%2 would be 1.
ChunLing #4
Posted 20 November 2012 - 04:38 PM
I guess I feel suspicious of %, though I suppose it only matters if you're using negative numbers (which we are not).
Papsern #5
Posted 21 November 2012 - 01:53 AM
Thanks for the help, but could the '%' be used to do what my example said but instead of printing numbers it runs functions? I can see that my example might have been confusing but as I said:
I wouldn't be using it to print numbers
But anyway, thanks for the replies.
Kingdaro #6
Posted 21 November 2012 - 02:14 AM
Sure.


for i=1, 5 do
  if i%2 == 1 then
    doSomeFunction()
  else
    doSomeOtherFunction()
  end
end
ChunLing #7
Posted 21 November 2012 - 04:59 AM
I have to admit, it seems like it would be simpler to write something like

for i=1,2 do
  doSomeFunction()
  doSomeOtherFunction()
end
doSomeFunction()

But if instead of just alternating a couple of functions you are executing a regular pattern that has variations at given intervals, then it does make sense to use this method.
Papsern #8
Posted 21 November 2012 - 11:42 AM
Sure.


for i=1, 5 do
  if i%2 == 1 then
	doSomeFunction()
  else
	doSomeOtherFunction()
  end
end
Thanks for this, I think it will work for what I'm making.

I have to admit, it seems like it would be simpler to write something like

for i=1,2 do
  doSomeFunction()
  doSomeOtherFunction()
end
doSomeFunction()

But if instead of just alternating a couple of functions you are executing a regular pattern that has variations at given intervals, then it does make sense to use this method.
The problem with that is it will do both functions on every loop, and I need it to do the first function the first time it loops, then the second function the second time it loops, then the first function for the third loop etc. Also, the amount of times it loops is a user input in my program.
ChunLing #9
Posted 22 November 2012 - 02:42 AM
Yes, but it is still just alternating the functions. What I'm saying is that this makes more sense if there are other things in the loop that get done every time, otherwise just have half the loops and do each function in every loop. They still alternate.

Of course, it's a little weird to care about such a tiny difference in complexity for the same functionality. But I'm a little weird. And I'd guess that eventually you'll have the loop do some things every loop, while only doing certain things on given intervals. Which is a problem well suited to this solution.
Papsern #10
Posted 22 November 2012 - 04:02 AM
Yes, but it is still just alternating the functions. What I'm saying is that this makes more sense if there are other things in the loop that get done every time, otherwise just have half the loops and do each function in every loop. They still alternate.

Of course, it's a little weird to care about such a tiny difference in complexity for the same functionality. But I'm a little weird. And I'd guess that eventually you'll have the loop do some things every loop, while only doing certain things on given intervals. Which is a problem well suited to this solution.
Ok I see what you mean now, but I've already used the other method and it works fine for me. Thanks anyway though.