Simple Output
The very first thing you should learn is how to make an output. Most all programs use outputs, as they are very importantOne way to make an output is the 'print' function.
lua--'lua' opens the'try it' [lua] shell--'print' is a function--We will learn more about this later--For now, just write functions as follows--func 'sArg'print 'Hello, World!'--The '' represents the start and end of a line of text, or a string--Actually, you should use "", not ''exit ''You should see something like:
Hello, World!2The '2' is just a line count. It is not very important to us
Try It!
There are two other output functions: 'write' and 'term.write'Write will do the same as print, but won't add an extra line (print does that)term.write is like write, but unlike write and print, it is not auto-wrap (that's where text can't go offscreen, autwrap makes new lines when needed)Try making the text 'ABC' appear
answer
print 'ABC'
data types
Before I continue, you must learn about data types. A data type determines what a value (such as 'Hello, World!') can do.Data types are as followed:Strings: the 'quoted thingies', these can be concentaited.Numbers (123) and Floats (1.2): these can be multiplied, divided, and so on. Remember PEMDAS?Tables (Arrays?): these store more data values!nil: Nothing, the absence of a valieFunctions: things like print and write.Booleans: true or false?
Advanced 'simple' Output
There are more advanced outputs:
print'1×2'--1×2--2print '1' × '2'--error: attempt to multiply two stringsprint '1' .. '2'--12--2 print 1 .. 2--error: attempt to concentate number and numberprint 1+2--3--2print {}--error: cannot print tableprint textutils.serialize({'hi', 'hi2'})--{-- hi-- hi2--}--5print 'hi\nhi'--hi--hi--3print 'hi&\\nhi'--hi\nhi--2print print--error: attempt to print function
strings
There is a string API for thisYou can do many things in a string:
'\\' = '\''\' = error'\n' = newline'\t' = tab'stringA'..'stringB'= 'stringAstringB'tonumber '1' = 1tonumber 'k' = nil
try it!
Try making various string arangments
booleans
These are true or false
numbers
It's easy, 5+5=10. There is also a math api for this. You cannot concentate numbers, unless you do this:
tonumber(tostring(1)..tostring(2))
tables
There is a table API for thisA table stores mutiple vales.
t = {['k']=5,'hi'}t['x'] = 'hi'print t.xprint t[1]
functions
A function is a piece of code that is stored for repeated use:
function writeH(h)write h..'h'endwriteH 'haw'write 'k', '!'function secArg(...) local args = {...} write args[2]endsecArg(1,'gh')
try it
Try making and running a function
scopes
Ther is such a thing as scopes
var --Access anywhere in programlocal var --only access later in program_G['var']--Access anywhere, any fileenv['var'] --Goes to local env
input
Another useful thing is user input
edit loginargs = {...}print 'Your username is '..args1..' and your password is '..read('*')Save and close the editor
login usr--Pss [enter]--See what happens
try it
Try interacting with the user
yielding
If yourr code takes a while, it may spit out a nasty error. Prevent this by inserting
coroutine.yield()every here and there
loops
A loop is a repeating portion of codeThere are different types of loops:
while read() != 'end' do os.queueEvent('') coroutine.yield()endfor i=0, 5 do os.queueEvent('') os.pullEvent() print iendfor k,v in pairs({'hi', ['x'] = 0}) print 'Value '..tostring(k)..' is '..v[/namedspoiler loops]
logic operators
You mat have noticed '==' or '!=' in my code. These are logic operators
== is =!= is not =not is opposite= is greater than/equal to< and > are normal
if?
[namedspoiler more about local scopes]If an undefined variable or 'local' is used in a function or loop, then that variable is local to the loop. A workaround for this is to either use the global scope or predefine the variable as An if statment evaluates whether a boolean is true or not, and performs an action based off of that. [code]if boolean do--action 'true'elseif 'otr boolean'--action for otr booleanelse--False actionend
Edited by EveryOS2.0, 10 November 2016 - 04:51 PM.