Jump to content




Loop thats user defined

turtle computer lua

9 replies to this topic

#1 Falco

  • Members
  • 33 posts
  • LocationSOMEWHERE IN F*%KING MINECRAFT!!!

Posted 14 December 2012 - 07:34 PM

Basicly i need some help with a script that will only loop a certain amount of times via user input, im struggling to find any help on the internet, or maybe its my poor searching abilty.

Thanks, help will be much appricated. :)

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 14 December 2012 - 07:38 PM

do something like the following

times = read()
for i = 1, tonumber(times) do
  -- do stuff
end


#3 Falco

  • Members
  • 33 posts
  • LocationSOMEWHERE IN F*%KING MINECRAFT!!!

Posted 14 December 2012 - 07:41 PM

View PostTheOriginalBIT, on 14 December 2012 - 07:38 PM, said:

do something like the following

times = read()
for i = 1, tonumber(times) do
  -- do stuff
end

I'm very very new to programing in computercraft, would you mind explaining how i would use this in my programs? pretty please...

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 14 December 2012 - 08:01 PM

View PostFalco, on 14 December 2012 - 07:41 PM, said:

View PostTheOriginalBIT, on 14 December 2012 - 07:38 PM, said:

do something like the following

times = read()
for i = 1, tonumber(times) do
  -- do stuff
end

I'm very very new to programing in computercraft, would you mind explaining how i would use this in my programs? pretty please...

Ok so for starters we are declaring a new variable called "times".
We then are calling the read() function. This function is a system function that reads the input from the terminal and only continues the program after enter/return is pressed.
Next we have a for loop. We declare the variable "i" as the number for the loop, and assign it to 1. the comma is similar to the ; in other programming languages. we then call another system function called tonumber() which converts the string the user has entered to a number. this specifies the maximum for our for loop. end specifies where the loop will go back to the start.

for loops can also have another comma and a 3rd number, this number is the increment amount. for example

for i = 0, 6 do would mean it loops from 0 to 6 in the default increment of 1
for i = 0, 6, 2 do would loop from 0 to 6 in increments of 2
for i = 6, 0, -1 do would loop from 6 to 0 in decrements of 1

So in terms of your program you would have something like this

-- Code up to the point of where you want user input
times = read() -- the program will pause here until the user hits enter
for i = 1, tonumber(times) do -- here say if the user enters 5 if will loop from 1 to 5
  -- what you want the script to do
end -- end of loop
-- continue program if required

hope this helps :)

EDIT: I feel i should also point out that everything after -- in lua is ignored as -- tells the compiler its a comment
using --[[ will ignore all code until it finds ]]

#5 JoshhT

  • New Members
  • 64 posts
  • LocationAustralia

Posted 14 December 2012 - 08:03 PM

Can OP please post some more info on what he would like to accomplish?

#6 Falco

  • Members
  • 33 posts
  • LocationSOMEWHERE IN F*%KING MINECRAFT!!!

Posted 14 December 2012 - 08:07 PM

View PostTheOriginalBIT, on 14 December 2012 - 08:01 PM, said:

View PostFalco, on 14 December 2012 - 07:41 PM, said:

View PostTheOriginalBIT, on 14 December 2012 - 07:38 PM, said:

do something like the following

times = read()
for i = 1, tonumber(times) do
  -- do stuff
end

I'm very very new to programing in computercraft, would you mind explaining how i would use this in my programs? pretty please...

Ok so for starters we are declaring a new variable called "times".
We then are calling the read() function. This function is a system function that reads the input from the terminal and only continues the program after enter/return is pressed.
Next we have a for loop. We declare the variable "i" as the number for the loop, and assign it to 1. the comma is similar to the ; in other programming languages. we then call another system function called tonumber() which converts the string the user has entered to a number. this specifies the maximum for our for loop. end specifies where the loop will go back to the start.

for loops can also have another comma and a 3rd number, this number is the increment amount. for example

for i = 0, 6 do would mean it loops from 0 to 6 in the default increment of 1
for i = 0, 6, 2 do would loop from 0 to 6 in increments of 2
for i = 6, 0, -1 do would loop from 6 to 0 in decrements of 1

So in terms of your program you would have something like this

-- Code up to the point of where you want user input
times = read() -- the program will pause here until the user hits enter
for i = 1, tonumber(times) do -- here say if the user enters 5 if will loop from 1 to 5
  -- what you want the script to do
end -- end of loop
-- continue program if required

hope this helps :)

EDIT: I feel i should also point out that everything after -- in lua is ignored as -- tells the compiler its a comment
using --[[ will ignore all code until it finds ]]
W

Wow thank you very much for this information, I'll be sure to read it untill it sticks in my head... YOU ARE A GOD!!!
No seriously thank you very much. Is there anyway I can give rep or anything on this forum?

View PostJoshhT, on 14 December 2012 - 08:03 PM, said:

Can OP please post some more info on what he would like to accomplish?

Thanks for attempting to help me, but help has been found. :)

#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 14 December 2012 - 08:11 PM

View PostFalco, on 14 December 2012 - 08:07 PM, said:

Wow thank you very much for this information, I'll be sure to read it untill it sticks in my head... YOU ARE A GOD!!!
No seriously thank you very much. Is there anyway I can give rep or anything on this forum?

Haha, no problems, anytime, seriously, just PM me (PM through profiles).

Yes there is a way to give rep. As to how, that is something I don't know how to do. I know that there is the "Vote this post up" green arrow. And I think there may be a "star rating" thingy on profiles (Click the persons photo).

#8 Falco

  • Members
  • 33 posts
  • LocationSOMEWHERE IN F*%KING MINECRAFT!!!

Posted 15 December 2012 - 01:41 AM

View PostTheOriginalBIT, on 14 December 2012 - 08:11 PM, said:

View PostFalco, on 14 December 2012 - 08:07 PM, said:

Wow thank you very much for this information, I'll be sure to read it untill it sticks in my head... YOU ARE A GOD!!!
No seriously thank you very much. Is there anyway I can give rep or anything on this forum?

Haha, no problems, anytime, seriously, just PM me (PM through profiles).

Yes there is a way to give rep. As to how, that is something I don't know how to do. I know that there is the "Vote this post up" green arrow. And I think there may be a "star rating" thingy on profiles (Click the persons photo).

Thanks, I'll will try my hardest to give you REP of some sort, you are a fine addition to this very helpful community. :)

#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 15 December 2012 - 01:50 AM

View PostFalco, on 15 December 2012 - 01:41 AM, said:

View PostTheOriginalBIT, on 14 December 2012 - 08:11 PM, said:

View PostFalco, on 14 December 2012 - 08:07 PM, said:

Wow thank you very much for this information, I'll be sure to read it untill it sticks in my head... YOU ARE A GOD!!!
No seriously thank you very much. Is there anyway I can give rep or anything on this forum?

Haha, no problems, anytime, seriously, just PM me (PM through profiles).

Yes there is a way to give rep. As to how, that is something I don't know how to do. I know that there is the "Vote this post up" green arrow. And I think there may be a "star rating" thingy on profiles (Click the persons photo).

Thanks, I'll will try my hardest to give you REP of some sort, you are a fine addition to this very helpful community. :)

Why thank you kind sir or madam :)

#10 Falco

  • Members
  • 33 posts
  • LocationSOMEWHERE IN F*%KING MINECRAFT!!!

Posted 15 December 2012 - 03:29 AM

View PostTheOriginalBIT, on 15 December 2012 - 01:50 AM, said:

View PostFalco, on 15 December 2012 - 01:41 AM, said:

View PostTheOriginalBIT, on 14 December 2012 - 08:11 PM, said:

View PostFalco, on 14 December 2012 - 08:07 PM, said:

Wow thank you very much for this information, I'll be sure to read it untill it sticks in my head... YOU ARE A GOD!!!
No seriously thank you very much. Is there anyway I can give rep or anything on this forum?

Haha, no problems, anytime, seriously, just PM me (PM through profiles).

Yes there is a way to give rep. As to how, that is something I don't know how to do. I know that there is the "Vote this post up" green arrow. And I think there may be a "star rating" thingy on profiles (Click the persons photo).

Thanks, I'll will try my hardest to give you REP of some sort, you are a fine addition to this very helpful community. :)

Why thank you kind sir or madam :)

Sir :) :P





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users