16 -- You can omit parentheses if the argument is one string or table literal
17 print "Hello World" <--> print("Hello World")
18 dofile 'a.lua' <--> dofile ('a.lua')
19 print [[a multi-line <--> print([[a multi-line
21 f{x=10, y=20} <--> f({x=10, y=20})
28 t.a = function() ... end
30 t = { ["hello"] = 200 }
33 -- Remember, arrays are also tables
34 array = { "a", "b", "c", "d" }
35 print(array[2]) -- "b" (one-indexed)
36 print(#array) -- 4 (length)
46 for i = start,finish,delta do
49 for k,v in pairs(tab) do
57 if condition then break end
81 function myFunctionWithArgs(a, b)
87 anonymousFunctions(function()
91 -- Not exported in the module
92 local function myPrivateFunction()
96 function doAction(action, ...)
97 print("Doing '"..action.."' to", ...)
98 --> print("Doing 'write' to", "Shirley", "Abed")
101 doAction('write', "Shirley", "Abed")
105 mytable = { x = 2, y = function() .. end }
111 -- Syntactic sugar, these are equivalent:
115 mytable.y(mytable, a, b)
118 function X:y(z) .. end
119 function X.y(self, z) .. end
126 -- A metatable is simply a table with functions in it.
127 mt.__tostring = function() return "lol" end
128 mt.__add = function(b) ... end -- a + b
129 mt.__mul = function(b) ... end -- a * b
130 mt.__index = function(k) ... end -- Lookups (a[k] or a.k)
131 mt.__newindex = function(k, v) ... end -- Setters (a[k] = v)
133 -- Metatables allow you to override behavior of another table.
135 setmetatable(mytable, mt)
143 function Account:new(balance)
144 local t = setmetatable({}, { __index = Account })
146 -- Your constructor stuff
147 t.balance = (balance or 0)
151 function Account:withdraw(amount)
152 print("Withdrawing "..amount.."...")
153 self.balance = self.balance - amount
157 function Account:report()
158 print("Your current balance is: "..self.balance)
161 a = Account:new(9000)
162 a:withdraw(200) -- method call
170 ## Operators (and their metatable names)
172 -- Relational (binary)
173 -- __eq __lt __gt __le __ge
175 ~= -- Not equal, just like !=
177 -- Arithmetic (binary)
178 -- __add __sub __muv __div __mod __pow
181 -- Arithmetic (unary)
182 -- __unm (unary minus)
186 nil and false --> nil
187 false and nil --> false
198 -- __index(table, key)
202 -- __newindex(table, key, value)
206 -- __concat(left, right)
213 ## API: Global functions [(ref)](http://lua.gts-stolberg.de/en/Basis.php)
216 loadfile("hello.lua")
218 assert(x) -- x or (raise an error)
221 type(var) -- "nil" | "number" | "string" | "boolean" | "table" | "function" | "thread" | "userdata"
223 -- Does /not/ invoke meta methods (__index and __newindex)
224 rawset(t, index, value) -- Like t[index] = value
225 rawget(t, index) -- Like t[index]
228 setfenv(1, {}) -- 1: current function, 2: caller, and so on -- {}: the new _G
230 pairs(t) -- iterable list of {key, value}
231 ipairs(t) -- iterable list of {index, value}
238 'string'..'concatenation'
243 s:len() -- Just like #s
263 table.foreach(t, function(row) ... end)
265 table.insert(t, 21) -- append (--> t[#t+1] = 21)
266 table.insert(t, 4, 99)
272 ## API: Math [(ref)](http://lua-users.org/wiki/MathLibraryTutorial)
274 math.abs math.acos math.asin math.atan math.atan2
275 math.ceil math.cos math.cosh math.deg math.exp
276 math.floor math.fmod math.frexp math.ldexp math.log
277 math.log10 math.max math.min math.modf math.pow
278 math.rad math.random math.randomseed math.sin math.sinh
279 math.sqrt math.tan math.tanh
286 io.output(io.open("file.txt", "w"))
290 for line in io.lines("file.txt")
292 file = assert(io.open("file.txt", "r"))
299 http://www.lua.org/pil/13.html
300 http://lua-users.org/wiki/ObjectOrientedProgramming