145 posts
Location
the vast cosmos of my mind; binary!
Posted 26 November 2012 - 05:31 PM
i need help with basically what the title says
i don't know how to incrementing on counting, how to use the for statement and to count how many times a line or part of code runs
even though i am a semi-advanced programmer. i feel ashamed :(/>
could someone please help me i've been needing to use this type of logic/coding for a long long long long time :(/>
i really need a full to semi in-depth understanding on how to use it. i've look EVERYWHERE to try and learn how to use it :(/>
PLEASE HELP ME!!! :(/>
2088 posts
Location
South Africa
Posted 26 November 2012 - 05:51 PM
for i = 1, 10 do
print(i .. "_")
end
-- WIll print the number of i and a _ each time (10 times)
-- for 'for' statements you can have a third number
-- which is used for the step, not including it defaults
-- the step to one
so the above will be the same as this:
for i = 1, 10, 1 do
print(i .. "_")
end
-- You can go increments of more than 2 by doing this
for i = 1, 10, 3 do -- will go up every 3
print(i)
end
-- Output: 1. 4. 7, 10
-- Negative steps
for i = 10, 1, -1 do -- If the first number is larger than the second number, I think you do not have to put -1
145 posts
Location
the vast cosmos of my mind; binary!
Posted 26 November 2012 - 08:06 PM
ok but i had a program that i made where i needed to display the ore and the count of it and continuously count up by one on that ore for each time it recieved a rednet message that was equal to that ore. could you help me with that?
2005 posts
Posted 26 November 2012 - 11:27 PM
vartoincrement = vartoincrement+1
Sorry, but what exactly are you asking for here?
145 posts
Location
the vast cosmos of my mind; binary!
Posted 27 November 2012 - 10:32 AM
how to use the for statement to count up 1 every time the a program receives a rednet message.
and how to use incrementtion
and regular math and mathematics on how it applies to programming
1548 posts
Location
That dark shadow under your bed...
Posted 27 November 2012 - 10:37 AM
well math works in programming the same as it does in the real world, the syntax is just a little different as we do not have a square root symbol etc. take a look
here for the maths functions. to increment something every time you receive a message create a variable equal to 0, make a loop listening for messages and every time it gets a message it adds 1 to your variable
rednet.open(modemside)
local count=0
while true do --infinite loop
print(count) --so you can see how high it has counted
rednet.receive()
count=count+1 --increment
end
2005 posts
Posted 29 November 2012 - 08:07 AM
Of note, don't use a for loop. That just ensures that your loop will eventually stop running.
2088 posts
Location
South Africa
Posted 29 November 2012 - 01:20 PM
rednet.open("side")
local count=0
while true do --infinite loop
print(count) --so you can see how high it has counted
senderID, msg = rednet.receive()
count = count + 1 --increment
end
What KaoS said but he failed with the rednet.receive() :> And yeah, for this you will need to use a infinite loop, while true do.
1548 posts
Location
That dark shadow under your bed...
Posted 29 November 2012 - 01:32 PM
well technically speaking you do not have to save the output from the receive function, it will still wait for a rednet message and then count. although it is in fact common practice to log the messages as well
2005 posts
Posted 30 November 2012 - 10:26 AM
I think that you want to maintain several different counts, and only increment a count in a given loop if you receive a message relating to that particular count. So:
local count,senderID,msg = {0,0,0,0} --however many counts you need
rednet.open("side")
while true do --infinite loop
for i = 1,4 do print(count[i]) end --so you can see how high it has counted
senderID, msg = rednet.receive()
if conditionalstatement then
--somehow extract an index n
count[n] = count[n] + 1 --increment
end
end
I can't say exactly what conditionalstatement should be without knowing the format of the messages the turtles are sending, but it shouldn't be too hard. Getting the right index for each count is the thing, if you're using turtles with IDs of 1-4 (or some other sequential group of IDs) and sorting the counts by turtle, then it's super easy. If you want to extract some element of the message that specifies which count should be increased, then it is trickier but still very doable.