User:Robpinkfish/sandbox

Example code[edit] The classic hello world program can be written as follows:

print("Hello World!") It can also be written as

io.write('Hello World!\n') or, the example given on the Lua website

io.write("Hello world, from ", _VERSION, "!\n") Comments use the following syntax, similar to that of Ada, Eiffel, Haskell, SQL and VHDL:

-- A comment in Lua starts with a double-hyphen and runs to the end of the line.

-- Multi-line strings & comments    are adorned with double square brackets.

--[=[ Comments like this can have other --comments nested. ]=] The factorial function is implemented as a function in this example:

function factorial(n) local x = 1 for i = 2, n do   x = x * i  end return x end Loops[edit] Lua has four types of loops: the while loop, the repeat loop (similar to a do while loop), the numeric for loop, and the generic for loop.

--condition = true

while condition do --statements end

repeat --statements until condition

for i = first,last,delta do    --delta may be negative, allowing the for loop to count down or up  --statements --example: print(i) end The generic for loop:

for key, value in pairs(_G) do print(key, value) end would iterate over the table _G using the standard iterator function pairs, until it returns nil.

Functions[edit] Lua's treatment of functions as first-class values is shown in the following example, where the print function's behavior is modified:

do local oldprint = print -- Store current print function as oldprint function print(s) -- Redefine print function, the usual print function can still be used        through oldprint. The new one has only one argument. oldprint(s == "foo" and "bar" or s) end end Any future calls to print will now be routed through the new function, and because of Lua's lexical scoping, the old print function will only be accessible by the new, modified print.

Lua also supports closures, as demonstrated below:

function addto(x) -- Return a new function that adds x to the argument return function(y) --[=[ When we refer to the variable x, which is outside of the current scope and whose lifetime would be shorter than that of this anonymous function, Lua creates a closure.]=] return x + y end end fourplus = addto(4) print(fourplus(3)) -- Prints 7

--This can also be achieved by calling the function in the following way: print(addto(4)(3)) -- This is because we are calling the returned function from `addto(4)' with the argument `3' directly.    This also helps to reduce data cost and up performance if being called iteratively. A new closure for the variable x is created every time addto is called, so that each new anonymous function returned will always access its own x parameter. The closure is managed by Lua's garbage collector, just like any other object.

Tables[edit] Tables are the most important data structures (and, by design, the only built-in composite data type) in Lua, and are the foundation of all user-created types. They are conceptually similar to associative arrays in PHP, dictionaries in Python and hashes in Ruby or Perl.

A table is a collection of key and data pairs, where the data is referenced by key; in other words, it's a hashed heterogeneous associative array. A key (index) can be any value but nil and NaN. A numeric key of 1 is considered distinct from a string key of "1".

Tables are created using the {} constructor syntax:

a_table = {} -- Creates a new, empty table Tables are always passed by reference (See Call by sharing):

a_table = {x = 10} -- Creates a new table, with one entry mapping "x" to the number 10. print(a_table["x"]) -- Prints the value associated with the string key, in this case 10. b_table = a_table b_table["x"] = 20  -- The value in the table has been changed to 20. print(b_table["x"]) -- Prints 20. print(a_table["x"]) -- Also prints 20, because a_table and b_table both refer to the same table.