Console
Operators
Variables
Strings
Tables
Functions
Flow of Control
Other Items of Interest
Operation Operator Notes String concatenation .. "Hello " .. "World"
("Hello " + " World" will NOT work)Equality == if (x == y) Inequality ~= if (x ~= y) Not not <, >, <=, >= <, >, <=, >= Work with both numeric and string values AND and AND and OR are unusual if the arguments are not boolean.
x and y -- returns x if x != false, otherwise returns y
x or y -- returns x if x == false (or nil), otherwise returns y
This can be used to default values. For example, a = b or "default"
Also, x and y or z equates to a ? b : cOR or Null test == nil
x,y = MyFunction()
-- Function returns two valuesx,y = y,x
-- This is an exception to the rule and is guaranteed to work as hoped.x,x = 1,2
-- Cannot say whether x will be 1 or 2a,b = b,c
-- Cannot say what the value of b will bea,b,c = 1,2
-- c is set to nil
Function Description tostring(anything) Converts anything to a string string.format(format, args) Supported template chars: %s = string
%d = decimal
%c = char
%f = floating point number (%g = compact fp number)
%x = hex (%X = uppercase hec)string.find(s1, s2) Returns the index of s2 in s1 string.len("abcde") Returns the string length string.lower("ABCDE") Converts to lowercase string.upper("abcde") Converts to uppercase string.sub("abcde", from)
string.sub("abcde", from, to)Returns string subsections
x = myTable.field1
x = myTable[1]
-- tables are 1-basedy = { } -- Creates an empty table
Function Description table.insert(myTable, newValue) APPENDs newValue table.insert(myTable, newValue) Inserts newValue table.insert(myTable, index, newValue) Get the size of the table table.getn(myTable) Calls function for each item in the table table.foreachi(mytable, function) Based on the index of the item. Note the trailing 'i' on foreachi. t = {1,2,3,4,5} table.foreach(mytable, function) Calls function for each item in the table based on the key of the item (myTable["key"] = "123" or t = {a="1", b="2"}) table.sort(myTable, function(a,b) return a>b end) Sorts the table using the logic in the provided function.
return 1,2;
end
x = (TwoValues()); -- x = 1
e.g x = (myFunction(1, 20))
Functions can be declared in two ways: function funcName(args) ... end; funcName = function(args) ... end; and args of string literial '...' indicates a variable number of arguments (which will be represented by a table called args, e.g. function(a, ...) called with (1, 2, 3, 4) would give arg[2] of 3. [TODO prove]
...
[else
... ]
end;
...
end
;for
key, value in {1, key2="value2", 3, 4} do...
end
;...
end
repeat
...
until
expressionIsTrue;