LUA

Brief Introduction to LUA

LUA 5.3 Reference Manual

String

string = "cat,dog"

one, two = string:match("([^,]+),([^,]+)")

Split a String

blob =a:1;b:2


KV = {}

for pair in string.gmatch(blob, "([^;]+),?") do

key, value = string:match("([^:]+),([^:]+)")

KV[key] = value

end

Parsing

values = "a1,a2,b1"

for val in string.gmatch(values, "([^,]+),?") do

-- val

end

Dictionary

blob =a:1;b:2


KV = {}

for pair in string.gmatch(blob, "([^;]+),?") do

key, value = string:match("([^:]+),([^:]+)")

KV[key] = value

end

You must use pairs instead of ipairs.

tab={} tab[1]='a' tab[2]='b' tab[5]='e'for k, v in pairs(tab) do print(k, v) end

Will output (in any order):

1 a 2 b 5 e

ipairs iterates over sequential integer keys, starting at 1 and breaking on the first nil pair.

pairs iterates over all key-value pairs in the table. Note that this is not guaranteed to iterate in a specific order.

Copy a LUA Table by value

To play a little readable-code-golf, here's a short version that handles the standard tricky cases:

  • tables as keys,

  • preserving metatables, and

  • recursive tables.

We can do this in 7 lines:

function copy(obj, seen) if type(obj) ~= 'table' then return obj end if seen and seen[obj] then return seen[obj] end local s = seen or {} local res = setmetatable({}, getmetatable(obj)) s[obj] = res for k, v in pairs(obj) do res[copy(k, s)] = copy(v, s) end return res end

There is a short write-up of Lua deep-copy operations in this gist.

Another useful reference is this Lua-users wiki page, which includes an example on how to avoid the __pairs metamethod.

Condition

If

if x == y or y~= z and r < s

-- do work

end

For

Json support

The json support doesn't come native. It comes via cjson library

cjson manual

cjson installation

local json = require( "cjson" ) -- Include the Corona JSON library

local t1 = '{"sid" : "sensor1" , "pfile" : "production"}'

local tab = {}

tab["sid"] = "sensor2"

local en = json.encode(tab)

print( en)

local dec = json.decode(t1)

print(dec["sid"])

Base64 API

base64.lua

Modules

What is a Module

Creating Modules

Set Package Path

function script_path()

local str = debug.getinfo(2, "S").source:sub(2)

return str:match("(.*/)")

end

local curDir = script_path()

-- ';opt/mypath/?.lua'

local luaPath = ';' .. curDir .. '?.lua'

-- set the path to lua files folder

package.path = package.path .. luaPath

require "core"

local utilClass = require "util"

LUA Gotchas

Gotchas

LUA String Patterns

local id = string.match(my_id, "(%w+)")

if (string.len(tostring(my_id)) == MY_ID_LENGTH and (my_id) == (id)) then

Patterns Tutorial

String Library

Date

Days diff

local start_date = '2016/04/30'

local end_date = '2015/04/30'

local sy, sm, sd = start_date:match("(%d+)/(%d+)/(%d+)")

local ey, em, ed = end_date:match("(%d+)/(%d+)/(%d+)")

local stime = os.time({year = sy, month = sm, day = sd})

local etime = os.time({year = ey, month = em, day = ed})

local timediff = etime - stime

local daysDiff = math.floor(timediff / (24 * 60 * 60)) -- 24 hours, 60 min, 60 seconds.

-- daysDiff is 365