Skip to content

Instantly share code, notes, and snippets.

@kousherAlam
Last active February 20, 2019 06:50
Show Gist options
  • Save kousherAlam/795e897fcb0fe2457322852cc8628749 to your computer and use it in GitHub Desktop.
Save kousherAlam/795e897fcb0fe2457322852cc8628749 to your computer and use it in GitHub Desktop.

Lua Syntax

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).

Comment

single linke comment start with -- multiline comment start with --[[ and end with ]]

The following keywords are resered and cannot be used as name
 and      break    do        else     elseif
 end       false     for       function  if
 in        local     nil       not      or
 repeat    return    then      true      until     while
Defining functions
function name(parameter)
    return parameter
end
print(name(20))
booleans

Only false and nilevaluate as false, everything else including 0 and empty string evaluate as true

garbage-collection
tab = {"lots", "of", "data"}
tab = nil; collectgarbage()
tables

ssigning tables does not copy its content, only the reference. tab1 = {"a", "b", "c"} Table index start from 1

conditions
if (condition) then
    -- do something
elseif (other_condition) then
    -- do something else
else
    -- do something
end
for loops

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

do blocks
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

function name_of_the_function(args)
     -- do something herer
end

Repeat

repeat 
     -- do something here 
until 10

While

Lua table ,

table constructor in lua is {} it's also define a array or sequence of element. Arrary start with 0 index not 1.

The colon operator :

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, {})

Luci disable production cache

uci set luci.ccache.enable=0; uci commit luci

Mqtt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment