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

A Simple Tutorial For The Newbies

Started by jay5476, 11 August 2013 - 05:44 AM
jay5476 #1
Posted 11 August 2013 - 07:44 AM
working with strings, and variables
this is a tutorial on how to work with different things in lua

"string" -- is a string
var = "string" -- is a variable containing the string "string"
table = {"string1", "string2"} -- now a table is defined by curly braces {}
now the above isnt very useful you need to now how to work with these,
strings:
Spoilerstrings are good for use in simple programs like

print("string") -- string is inside the " "
you can use that to print the word string or what evers inside the " "
you can use strings in any function that expects a string, like print()
variables:
Spoilervariables are awesome they an contain your string or table for later use.
how to use

var = "my var"
print(var) -- prints 'my var'
print(var) -- prints 'my var'
var = "different" -- this changes what is inside the variable, overrides it
print(var) -- prints 'different'
now this is more efficent then having to keep typing 'my var' or 'different'
but this can also be used to print a changing variable like:

if rs.getInput("right") then
var = "on" -- if right side powered by redstone var will be 'on'
else
var = "off" -- if right side not powered by redstone var will be 'off'
end
print(var) -- print the variable
now you can see how this is useful to print a conditional statement,
now cool thing is about variables the can also get information from functions like:

program = shell.getRunningProgram() -- shell.getRunningProgram is a defualt program that returns the running program ( stores it in the variable )
print(program) -- will print the running program
now any string including those returned by functions can be stored in a variable
working with functions, tables and loops
Spoilernow functions, tables and loop can help shorten code ( this will be sorta all worked in together were needed )
first of functions and args:
functions are things in a program you can call to run a code

-- define a function like this
function myFunction(args) -- args is a variable to be called upon later in a variable
-- enter code you want to run when function is called
print(args) -- prints 'args'which is defined when functions called
end -- you have to end a function with an end
-- to call function just type in its name and args like this
myFunction("Hello") -- this will print 'hello' which is defined as args when function is called
-- you also have to call it for the code to run
functions can also be used to return a variable like:

-- how to use our other program as a function
function redstone() -- no args are used in this function
if rs.getInput("right") then
var = "on" -- if right side powered by redstone var will be 'on'
retunr var -- returns 'on'
else
var = "off" -- if right side not powered by redstone var will be 'off'
return var -- -- returns 'off'
end -- end 'if'
end -- end function
-- now we can get var because of return
returned = redstone() -- returned = what the function redstone returned
print(returned) -- prints returned
now onto tables and loops:
Spoilernow tables are like variables they store information but, they can store multiple strings like:

table = {[1] = "hello", [2] = "world"} -- use curly braces to define it as a table and ',' to seperate strings.
now thats a table but its a little more complecated if we want to print whats in the table heresd were for loops come in handy

-- we could do
table = {[1] = "hello", [2] = "world"}
print(table[1]) -- which would print 'hello' because in table [1] = "hello", we could also change this to print [2]
print(table[2]) -- this would print all in the table but it is not good enough if:
table = {[1] = "a", [2] = "b", [3] = "c"}
-- this would not print everything in the table, if we want to do that we can use for loops
use for i = 1, 10 do to loop the code 10 times

for i = 1, #table do -- #table is how many variables table contains, it returns a number
print(table[i]) -- i = the number of times it has looped so on the first time table[i] is really table[1]
end -- close your for loop
-- you could also use pairs
for key, value in pairs(table) do -- now there is to variables defined by this function 'key' and 'value'
--[[ now basicly
key = number of looped times
value = table[key]
]]--
print(value) -- print the current variable in table
end -- close your for loop
now they can be useful for display all your tables but, you might think for 3 variables thats alot but it will run for any amout of variables you have
thank you for reading this tutorial and if you have any questions please post below
i might be adding more later
RandomMcSomethin #2
Posted 14 August 2013 - 08:44 PM
Thx for the loops! I need to learn more, for I am a feeble newbie.

for i=10 do
print "I'm a noob"
end
Lupus590 #3
Posted 10 September 2014 - 02:25 PM
Could you space out your code a bit? It feels like a lot of walls of code.
Adding extra blank lines at points where you end a loop and start another point will help people read your code easier (i.e. "oh, a blank line, that means that we are moving on in to a new section of code")

Another tip, put indents in your loops, readers can then easily identify what's in the loop and what is not.

Example with both tips implemented:

for i = 1, #table do -- #table is how many variables table contains, it returns a number
	 print(table[i]) -- i = the number of times it has looped so on the first time table[i] is really table[1]
end -- close your for loop

-- you could also use pairs
for key, value in pairs(table) do -- now there is to variables defined by this function 'key' and 'value'
	 --[[ now basicly
	 key = number of looped times
	 value = table[key]
	 ]]--
	 print(value) -- print the current variable in table
end -- close your for loop
Edited on 10 September 2014 - 12:27 PM