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

Help Me Please

Started by rifleman23, 21 January 2014 - 02:24 PM
rifleman23 #1
Posted 21 January 2014 - 03:24 PM
why doesn't this loop work!

i have tried to fix it but simply do not possess the required knowledge in lua or any programming to solve this problem that seems to occur every time i proceed to test my new wall building api. i have tried to create a way of writing in the dimensions of a wall ie. height and length however a problem arises

j represents variable written in ui by person

for k=1,j do – represents how long the wall should be if it had worked
turtle.forward()
………. and so on

please help it says i need an equals sign but i have no idea where or what to do in this case

thank you
Lyqyd #2
Posted 21 January 2014 - 04:18 PM
Please post the whole code and the full error message.

Also check out the Common Errors section of the Read This Post Before Asking Questions sticky, it may elucidate.
surferpup #3
Posted 22 January 2014 - 11:19 AM
Posting the whole code would allow us to give you better help.

For now, troubleshoot the problem by replacing j with a number and see if that is really the source of your problem. Here I have replaced j with 5:


for k=1,5 do -- represents how long the wall should be if it had worked
turtle.forward()

.......... and so on
end


If it is still broken, it is not the for loop itself.
Edited on 22 January 2014 - 10:20 AM
TechMasterGeneral #4
Posted 22 January 2014 - 04:09 PM
Try putting a space
like this:
 k = 1, 5 do 

Lua can be a bit picky about that stuff
CometWolf #5
Posted 22 January 2014 - 04:13 PM
Try putting a space
like this:
 k = 1, 5 do 

Lua can be a bit picky about that stuff

it really isn't picky at all, and that makes no difference.

k=1,5 do
  print"derp"
end
would work just fine.
Edited on 22 January 2014 - 03:13 PM
Lyqyd #6
Posted 22 January 2014 - 05:01 PM
Try putting a space
like this:
 k = 1, 5 do 

Lua can be a bit picky about that stuff

This is completely incorrect. The spacing between those things is completely irrelevant. As long as there is a space after `for` and a space before `do`, the rest of it is fine. In a generic for loop, there must also be a space on either side of the `in`. Please don't add blatantly incorrect information, as it can mislead people and spread confusion.
surferpup #7
Posted 24 January 2014 - 12:41 AM
On arithmetic for loops (loops which count rather than serve as iterators for a list), this is the syntax:


for <loop variable> = <start value> , <end value> < , optional step value >  do
<executable statements>
end

The parameters above are as follows:
  • loop variable – any legitimate name for a variable. It will be considered a local variable accessible within the for loop only.
  • start value – any numeric value or numeric variable. If it is a variable, it must be defined with a value (undefined variables will through an error).
  • end value – any numeric value or numeric variable. If it is a variable, it must be defined with a value (undefined variables will through an error).
  • step value – any numeric value or numeric variable. This is a value by which the loop variable will be incremented or decremented. This parameter is optional, although if it is included, it must be preceded by a comma. If this parameter is not included, the loop variable will be incremented by 1.
Other considerations regarding the for loop:
  1. The loop variable is assigned the start value immediately upon execution of the for statement. This occurs only once.
  2. The conditional test (has the loop variable incremented or decremented beyond the end value?) is performed after the initial assignment in the previous step.
  3. If the conditional test is false at the beginning of the loop, the loop will exit and the program counter will jump to the first statement following the end statement.
  4. The loop variable holds its value throughout the code block until it is changed when the program counter reaches the end statement.
  5. There must be a do following the for definition as shown. This marks the beginning of the code block.
  6. There must be an end as the last statement of the loop. It marks the end of the code block.
  7. The loop variable is incremented or decremented (depending on the step value) when the program counter reaches the end statement and comes back up into the for statement at the point of the conditional test (#2 in this list)
Both of these are legitimate for loops:


print ("Result One")

for k = 1, 5 do
  print (k)
end

print ("Done with One")
print("")

print ("Result Two")
for myLoopVariable = 7,-3,-2 do
  print (myLoopVariable)
end

print ("Done with Two")

The above code will produce the following output:

Result One
1
2
3
4
5
Done with One

Result Two
7
5
3
1
-1
-3
Done with Two
Try this yourself and see what you find.
Edited on 26 January 2014 - 02:56 AM