Posted 10 November 2016 - 02:59 AM
Here is a guide for new programmers: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)
Spoiler
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!2
The '2' is just a line count. It is not very important to usSpoiler
Try making the text 'ABC' appearSpoiler
print 'ABC'Spoiler
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?Spoiler
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
Spoiler
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
Spoiler
Try making various string arangmentsSpoiler
These are true or falseSpoiler
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))
Spoiler
There is a table API for thisA table stores mutiple vales.t = {['k']=5,'hi'}t['x'] = 'hi'print t.xprint t[1]
Spoiler
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')
Spoiler
Try making and running a functionSpoiler
Ther is such a thing as scopesvar --Access anywhere in programlocal var --only access later in program_G['var']--Access anywhere, any fileenv['var'] --Goes to local env
Spoiler
Another useful thing is user inputedit loginargs = {...}print 'Your username is '..args1..' and your password is '..read('*')
Save and close the editorlogin usr--Pss [enter]--See what happens
Spoiler
Try interacting with the userSpoiler
If yourr code takes a while, it may spit out a nasty error. Prevent this by insertingcoroutine.yield()
every here and thereSpoiler
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]Spoiler
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
Spoiler
An if statment evaluates whether a boolean is true or not, and performs an action based off of that.if boolean do--action 'true'elseif 'otr boolean'--action for otr booleanelse--False actionend
Spoiler
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 asEdited on 10 November 2016 - 03:51 PM