Difference between revisions of "NETIO Lua Manual"

From wiki.netio-products.com
Jump to navigation Jump to search
Line 79: Line 79:
  
 
Now return to the most common example above using <code>pairs()</code> Lua function and read about <code>pairs()</code> and <code>ipairs()</code> implementation [https://www.lua.org/pil/7.3.html here].
 
Now return to the most common example above using <code>pairs()</code> Lua function and read about <code>pairs()</code> and <code>ipairs()</code> implementation [https://www.lua.org/pil/7.3.html here].
 +
 +
== Functions ==
 +
 +
Lua allows multiple results (comma separated):
 +
 +
function diskSpace()
 +
  return devices.system.freeSpace, devices.system.totalSpace
 +
end
 +
 +
logf("Free space: %d MB", diskSpace())
 +
local free,total = diskSpace()
 +
logf("Free space: %d %%", 100*free/total)
 +
 +
Variable-length arguments are also available:
 +
 +
function sum(...)
 +
  local result = 0
 +
  for _,v in ipairs(arg) do
 +
    result = result + v
 +
  end
 +
  return result
 +
end
 +
 +
logf("Sum: %d", sum(2,3,5))

Revision as of 12:38, 25 April 2017

Debugging

To debug your code, use log() and logf() functions. See the output in system events log.

Conditions

Lua conditions has usual syntax, see here. Just keep in mind to merge else and if to elseif or multiple ends are required.

Loops

Lua has break statement, but no continue (use condition inside loop instead).

While

While syntax is not different to other languages:

local i = 0
while i~=3 do
  i = i + 1
  logf("%d",i)
end

Repeat until

Repeat until syntax is the same as Pascal's, other languages have similar do-while loops:

local i = 0
repeat
  i = i + 1
  logf("%d",i)
until i==3

Numeric for

local arr = {2,3,7,5}
for i=1,#arr do logf("%d",arr[i]) end

for initVar,limit,increment do

  • number assignment initVar inits loop-local variable
  • number limit loops until initVar reaches this value
  • optional number increment after each loop initVar increment by this value (default 1)

Generic for

Is rather sophisticated. Most common example (the order of elements in pairs() is not guaranteed):

local tab = {one=1, two=2, three=3}
for key,val in pairs(tab) do logf("%s:%d",key,val) end

Generic for syntax

for var_1, ..., var_n in explist do block end

is equivalent to (Full explanation here.)

do
  local _f, _s, _var = explist
  while true do
    local var_1, ... , var_n = _f(_s, _var)
    _var = var_1
    if _var == nil then break end
    block
  end
end

Iterator closure that holds its state

function iter(a)
  local i = 0
  return function()
    i = i+1
    return a[i]
  end
end

local arr = {2,3,7,5}
for value in iter(arr) do
  logf("%d",value)
end

Stateless iterator (in this case returns variable list: key and value)

function iter(a,i)
  i = i+1
  if a[i] then return i,a[i] end
end

local arr = {2,3,7,5}
for k,v in iter,arr,0 do
  logf("%d:%d",k,v)
end

Same effect using ipairs() Lua function (without initial state)

for k,v in ipairs(arr) do
  logf("%d:%d",k,v)
end

Now return to the most common example above using pairs() Lua function and read about pairs() and ipairs() implementation here.

Functions

Lua allows multiple results (comma separated):

function diskSpace()
  return devices.system.freeSpace, devices.system.totalSpace
end

logf("Free space: %d MB", diskSpace())
local free,total = diskSpace()
logf("Free space: %d %%", 100*free/total)

Variable-length arguments are also available:

function sum(...)
  local result = 0
  for _,v in ipairs(arg) do
    result = result + v
  end
  return result
end

logf("Sum: %d", sum(2,3,5))