Lua is a dynamically typed language. There are no type definitions in the language. All values carry their own type. All values in Lua are first-class values. This means that all values can be stored in variables, passed as arguments to other functions, and returned as results
There are eight basic types in Lua: nil, boolean, number, string, function, userdata, thread, and table.
The type table implements associative arrays, that is, arrays that can be indexed not only with numbers, but with any value (except nil). Tables can be heterogeneous; that is, they can contain values of all types (except nil).
single linke comment start with --
multiline comment start with --[[
and end with ]]
and break do else elseif
end false for function if
in local nil not or
repeat return then true until while
function name(parameter)
return parameter
end
print(name(20))
Only false
and nil
evaluate as false, everything else including 0
and empty string evaluate as true
tab = {"lots", "of", "data"}
tab = nil; collectgarbage()
ssigning tables does not copy its content, only the reference.
tab1 = {"a", "b", "c"}
Table index start from 1
if (condition) then
-- do something
elseif (other_condition) then
-- do something else
else
-- do something
end
There are two types of for loop in Lua: a numeric for loop and a generic for loop.
for a=1, 10, 2 do -- for a starting at 1, ending at 10, in steps of 2
print(a) --> 1, 3, 5, 7, 9
end
The third expression in a numeric for loop is the step by which the loop will increment. This makes it easy to do reverse loops. If the step expression is left out, Lua assumes a default step of 1.
Generic for loops work through all values that an iterator function returns:
for key, value in pairs({"some", "table"}) do
print(key, value)
--> 1 some
--> 2 table
end
Lua provides several built in iterators (e.g., pairs , ipairs ), and users can define their own custom iterators as well to use with generic for loops
local a = 10
do
print(a) --> 10
local a = 20
print(a) --> 20
end
print(a) --> 10
- Literal strings can be delimited by matching single or double quotes,
- Can contain the following C-like escape sequences
- Literal strings can also be defined using a long format enclosed by long brackets.
function name_of_the_function(args)
-- do something herer
end
repeat
-- do something here
until 10
table constructor in lua is {}
it's also define a array or sequence of element. Arrary start with 0
index not 1
.
It is a syntaxtic sugar in lua. It make function call look like object call. such as ...
res.template{}
Is Equvlant to
res.template(res, {})
uci set luci.ccache.enable=0; uci commit luci