Lua
Lua is a scripting language that is very lightweight and at the same time powerful. Although it's not as widely known as Perl or Python, it's used as an extension language in many projects and commercial products, probably because of it's small footprint, simplicity and good performance.
Contents |
[edit] Quick Tutorial
If you already have scripting/programming experience, it shouldn't take more than a couple of hours (or minutes) to be able to understand and write code in Lua. If you have lua installed, you can start the interpreter and type commands directly to execute them. From MySQL Workbench you can press Ctrl + F3, which will open a command interpreter for its internal scripting system, which supports Lua scripting.
[edit] A Simple Program
TODO: Add a simple program here.
[edit] Data Types
Main data types:
- boolean (true, false)
- strings (ex.: 'like this' or "like this")
- integers (ex.: 1234)
- floating-point numbers (ex.: 123.456)
- tables can be used as arrays or dictionaries/key->value mappings (ex.: {1,2,3} or {name:"joe", age:32}). Array indices start at 1.
[edit] Control Structures
- IF
if <expr> then <stuff> else <stuff> end
Ex.:
if (var > 50) and (flag ~= 0) then
print("Something")
elif (var > 30) and (flag ~= 0) then
print("Something else")
else
print("Another something else")
end
- FOR
for <counter>=<start>,<end> do <stuff> end
Ex.:
for i= 1, 10 do
print("Hello")
for j= i, 1, -1 do
print("!")
end
end
- WHILE
while <expr> do <stuff> end
Ex.:
while i < 10 do
i= i+1
if some_condition == true then
break
end
end
[edit] Expressions and Operators
1+1 1+1 ~= 3 1 > 2 1 < 2 1 <= 2 2+2 == 4 true and false or not true "Hello".." ".."World" --> will give "Hello World"
[edit] Functions
function foo(arg1, arg2, arg3) <stuff> return result end
[edit] Some Handy Functions
print("Hello World")
string.sub("Hello World", 7, 9) --> will give 'Wor'
table.getn({32,4321,32,56}) --> 4 (number of elements in table)
table.insert({3,5,6}, 2, 4) --> {3,4,5,6}