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

Iterations?

Started by lordofttude, 21 December 2014 - 08:58 PM
lordofttude #1
Posted 21 December 2014 - 09:58 PM
If I had a program say "mine", how would I make it so that I could say "mine 10" to make that program iterate 10x, or "mine 2" to make the program iterate 2x and so on and so forth? Also How would I then make it display what iteration it was on? Thanks.
TheOddByte #2
Posted 21 December 2014 - 10:09 PM
You could do this( I believe you meant getting arguments when running the program right?

local args = { ... }
if #args < 1 then
	error( "Usage: mine <n>", 0 )
end

local n = tonumber( args[1] )
for i = 1, n do
	-- do stuff
end
by this example you could do "mine 10"

If you want todo it by a read input or something you could do this

local function getArguments( str )
	local arguments = {}
	for word, _ in str:gmatch( "%S+" ) do
		table.insert( arguments, word )
	end
	return arguments
end

local input = read()
local args = getArguments( input )
if #args < 2 then
	error( "Usage: mine <n>", 0 )
end
if args[1] == "mine" then
	local n = tonumber( args[2] )
	for i = 1, n do
		-- do stuff
	end
end

You should read about for loops here to get a better understanding.

Displaying what iteration it's on is simple

--# The loop variable's name is 'i', which we can simply print
for i = 1, 10 do
    print( i )
end
Edited on 21 December 2014 - 09:21 PM
lordofttude #3
Posted 21 December 2014 - 10:19 PM
You could do this( I believe you meant getting arguments when running the program right?

local args = { ... }
if #args < 1 then
	error( "Usage: mine <n>", 0 )
end

local n = tonumber( args[1] )
for i = 1, n do
	-- do stuff
end
by this example you could do "mine 10"

If you want todo it by a read input or something you could do this

local function getArguments( str )
	local arguments = {}
	for word, _ in str:gmatch( "%S+" ) do
		table.insert( arguments, word )
	end
	return arguments
end

local input = read()
local args = getArguments( input )
if #args < 2 then
	error( "Usage: mine <n>", 0 )
end
if args[1] == "mine" then
	local n = tonumber( args[2] )
	for i = 1, n do
		-- do stuff
	end
end
Yes that is what I meant, thanks.
bcdaedalus #4
Posted 22 December 2014 - 11:12 PM
dont sapose u could pastebin the compleated code to save others some time who are trying t od this for the first time ever using computercraft:)
Cranium #5
Posted 22 December 2014 - 11:14 PM
dont sapose u could pastebin the compleated code to save others some time who are trying t od this for the first time ever using computercraft:)
Do at least try to spell check what comes from your keyboard…
TheOddByte #6
Posted 22 December 2014 - 11:17 PM
Do at least try to spell check what comes from your keyboard…
There's so many spelling errors there O.O
And for the one who asked for a pastebin link you can simply do it yourself.
Copy the code > Paste it onto pastebin
lordofttude #7
Posted 24 December 2014 - 09:30 PM
Hey, I was wondering how I could make it so if a computer receives a redstone signal on the bottom it would pause the program that is running until the signal turns off… I tried chucking an if not statement in but that isn't enough. Clearly I am very new to Lua and just programming as a whole, but can someone suggest something? Thanks.
Here is the code, pastebin get jjgfhxkT


local args = { .. }
if #args  <  1 then
			 error(  "Usage: frame <n>" , 0)
end

function moveFrame()
	  rs.setOutput("top", true)
	  sleep(0.5)
	  rs.setOutput("top, false)
end

function mine()
	  rs.setOutput("left", true)
	  sleep(6.5)
	  rs.setOutput("left", false)
end

local n = tonumber ( args[1] )
for i = 1, n do
	  move frame()
	  mine()
end

if not (rs.getInput("bottom")) then
   moveFrame()
   mine()
end
Edited on 24 December 2014 - 10:43 PM
Bomb Bloke #8
Posted 24 December 2014 - 09:49 PM
You've more or less got the idea, but you need to put that check into your mining loop. Eg:

local args = { ... }
local n = tonumber ( args[1] )
if not n then error( "Usage: frame <n>" , 0) end

for i = 1, n do
	while rs.getInput("bottom") do  -- So long as redstone is on...
		os.pullEvent("redstone")  -- ... wait for redstone state changes.
	end

	rs.setOutput("top", true)
	sleep(0.5)
	rs.setOutput("top", false)

	rs.setOutput("left", true)
	sleep(6.5)
	rs.setOutput("left", false)
end
Edited on 24 December 2014 - 08:52 PM
lordofttude #9
Posted 24 December 2014 - 10:01 PM
You've more or less got the idea, but you need to put that check into your mining loop. Eg:

local args = { ... }
local n = tonumber ( args[1] )
if not n then error( "Usage: frame <n>" , 0) end

for i = 1, n do
	while rs.getInput("bottom") do  -- So long as redstone is on...
		os.pullEvent("redstone")  -- ... wait for redstone state changes.
	end

	rs.setOutput("top", true)
	sleep(0.5)
	rs.setOutput("top", false)

	rs.setOutput("left", true)
	sleep(6.5)
	rs.setOutput("left", false)
end
Thank you very much…..that makes sense.
lordofttude #10
Posted 24 December 2014 - 10:42 PM
You've more or less got the idea, but you need to put that check into your mining loop. Eg:

local args = { ... }
local n = tonumber ( args[1] )
if not n then error( "Usage: frame <n>" , 0) end

for i = 1, n do
	while rs.getInput("bottom") do  -- So long as redstone is on...
		os.pullEvent("redstone")  -- ... wait for redstone state changes.
	end

	rs.setOutput("top", true)
	sleep(0.5)
	rs.setOutput("top", false)

	rs.setOutput("left", true)
	sleep(6.5)
	rs.setOutput("left", false)
end
Also, how would I make it print what iteration the program is on? As in if I said "frame 3" (frame being the name of the program) it would print 1 for the first iteration. Then as soon as the second iteration starts it would print 2, so on and so forth.
Someone told me it would be as simple as printing the variable "i" but that doesn't seem to work… it just lists 1 through 10.
Edit: Someone answered my question already :)/> thanks anyway.
Edited on 24 December 2014 - 10:43 PM
lordofttude #11
Posted 24 December 2014 - 10:50 PM
You could do this( I believe you meant getting arguments when running the program right?

local args = { ... }
if #args < 1 then
	error( "Usage: mine <n>", 0 )
end

local n = tonumber( args[1] )
for i = 1, n do
	-- do stuff
end
by this example you could do "mine 10"

If you want todo it by a read input or something you could do this

local function getArguments( str )
	local arguments = {}
	for word, _ in str:gmatch( "%S+" ) do
		table.insert( arguments, word )
	end
	return arguments
end

local input = read()
local args = getArguments( input )
if #args < 2 then
	error( "Usage: mine <n>", 0 )
end
if args[1] == "mine" then
	local n = tonumber( args[2] )
	for i = 1, n do
		-- do stuff
	end
end

You should read about for loops here to get a better understanding.

Displaying what iteration it's on is simple

--# The loop variable's name is 'i', which we can simply print
for i = 1, 10 do
	print( i )
end
When I ask it to print i it just lists 1 through 10… do you know what I did wrong?
MKlegoman357 #12
Posted 24 December 2014 - 11:16 PM
What did you expect it to do? You print the value of 'i' which is just a simple number. Also, please try to keep all problems with the same code in one thread.
lordofttude #13
Posted 24 December 2014 - 11:23 PM
What did you expect it to do? You print the value of 'i' which is just a simple number. Also, please try to keep all problems with the same code in one thread.
I will keep all the problems with the code the same thread, thanks for telling me, but this thread wasn't originally about a problem, it was just a question on how to do something and the help I received was only used in writing a piece of code later. That said how would I print what iteration the program is on, as in iteration 1, iteration 2, iteration, 3?
Edited on 24 December 2014 - 10:23 PM
MKlegoman357 #14
Posted 24 December 2014 - 11:27 PM
You could simply use the basic Lua operator '..' (concat):


print("iteration " .. i)
lordofttude #15
Posted 24 December 2014 - 11:32 PM
You could simply use the basic Lua operator '..' (concat):


print("iteration " .. i)
Thanks
KingofGamesYami #16
Posted 24 December 2014 - 11:41 PM
It sounds to me like we have a misunderstanding… What you want to do sounds like listing 1 through 10. Could you clarify what you mean?
Bomb Bloke #17
Posted 24 December 2014 - 11:44 PM
Worth noting that the same discussion is going on in this thread.
wieselkatze #18
Posted 24 December 2014 - 11:45 PM
With BB's code that would exactly be what you have to do. It would look like this:


local args = { ... }
local n = tonumber ( args[1] )
if not n then error( "Usage: frame <n>" , 0) end

for i = 1, n do
        print( "Iteration " .. i .. "/" .. n )
        while rs.getInput("bottom") do  -- So long as redstone is on...
                os.pullEvent("redstone")  -- ... wait for redstone state changes.
        end

        rs.setOutput("top", true)
        sleep(0.5)
        rs.setOutput("top", false)

        rs.setOutput("left", true)
        sleep(6.5)
        rs.setOutput("left", false)
end


As the 3 of 'frame 3' would get passed as a number as the end condition for the for loop, it would only iterate 3 times - not more.
Lyqyd #19
Posted 24 December 2014 - 11:45 PM
Threads merged.
lordofttude #20
Posted 24 December 2014 - 11:52 PM
It sounds to me like we have a misunderstanding… What you want to do sounds like listing 1 through 10. Could you clarify what you mean?
Yes, I thought that by printing "i" I could tell what iteration my program was on but that is not the case. Someone just clarified this for me though and this is what he said would work: print("iteration" .. i). Thanks for the help anyway :)/>.
lordofttude #21
Posted 25 December 2014 - 12:06 AM
With BB's code that would exactly be what you have to do. It would look like this:


local args = { ... }
local n = tonumber ( args[1] )
if not n then error( "Usage: frame <n>" , 0) end

for i = 1, n do
		print( "Iteration " .. i .. "/" .. n )
		while rs.getInput("bottom") do  -- So long as redstone is on...
				os.pullEvent("redstone")  -- ... wait for redstone state changes.
		end

		rs.setOutput("top", true)
		sleep(0.5)
		rs.setOutput("top", false)

		rs.setOutput("left", true)
		sleep(6.5)
		rs.setOutput("left", false)
end


As the 3 of 'frame 3' would get passed as a number as the end condition for the for loop, it would only iterate 3 times - not more.
Thanks