2007 Lua - Notes

I encourage you to participate in Lua communities:

Moje notatki (bardziej traktuj je ze są na brudno) z Lua.
Może z tego kiedyś tutorial lub ocś takiego wyrośnie :P.


Auxiliary library
lua_* and luaL_
Be aware that lua C-API functions begins from lua_ , but lua auxiliary library functions begins from luaL_ .
So if you have error like:

lua_simplebinding.cpp:114: error: ‘lua_dofile’ undeclared (first use this function)

Try to rename this function from lua_dofile to luaL_dofile.

Opening libraries begging from version 5.1
Begging from version 5.1 you can not call luaopen_ functions direclty.
But you can use auxiliary function to open all libraries luaL_openlibs.
More at manual chapter 5.

luaL_openlibs raise linkage errors?
No problem…
for errors like:

/…iblua.a(loadlib.o)(.text+0x11a):loadlib.c: undefined reference to ‘dlsym’
/…/liblua.a(loadlib.o)(.text+0x1d1):loadlib.c: undefined reference to ‘dlerror’
/…/liblua.a(loadlib.o)(.text+0x1ff):loadlib.c: undefined reference to ‘dlerror’
/../liblua.a(loadlib.o)(.text+0xe47): In function ‘gctm’:
loadlib.c: undefined reference to ‘dlclose’

Try to add linking option -ldl .

Vim support for Lua?

  • Vim comes with syntax highlighting and indentation for Lua. Vim 7 supports Lua 5.1
  • ftp.vim.org You can download the lastest version of the syntax and indent files here.
  • luarefvim is the Lua reference manual for Vim.
  • lua-support Lua-IDE for Vim.

But there were problems with indent so i found lua-users.org: # Subject: Vim syntax and indent for Lua 4 and 5
and here are two attachments:

Objects Cloning

Source : http://lua-users.org/wiki/RetiredLuaFaq
3.4 - How do I clone objects?
If you really want to copy a table, you can use the built-in function next, as in the code below:

    function clone(t)            -- return a copy of the table t
      local new = {}             -- create a new table
      local i, v = next(t, nil)  -- i is an index of t, v = t[i]
      while i do
        new[i] = v
        i, v = next(t, i)        -- get next index
      return new

If you want a deep copy , add if type(v)=="table" then v=clone(v) end before new[i] = v.


Więc tutaj użyteczne funkcje


function printf(...) io.write(string.format(unpack(arg))) end


My first serious function in Lua :P

function print_table_recursively(t,prefix)
    if prefix == nil then prefix = '' end
    local fx;
    fx = function(prefix,t)
        local i,v = next(t,nil);
        prefix = tostring(prefix);
        while i do 
            if( type(i) == 'number' ) then
                idesc = i;
                idesc = '"'..tostring(i)..'"';
            if( type(v) ~= 'table' ) then
                print ( prefix.."["..idesc.."] =\t"..tostring(v) )
                fx( prefix.."["..idesc.."]", v ) 
            i,v = next(t,i);

For that commands:

t = { [-1]='less then zero', [0]='zero', 'one', {1,2}, {q=0,p={Re=5,Im=3},{Re=1,Im=6}}, 'last' }

It provides such output:

tablename[1] = one
tablename[2][1] = 1
tablename[2][2] = 2
tablename[3][1]["Re"] = 1
tablename[3][1]["Im"] = 6
tablename[3]["q"] = 0
tablename[3]["p"]["Re"] = 5
tablename[3]["p"]["Im"] = 3
tablename[4] = last
tablename[-1] = less then zero
tablename[0] = zero


My second serious Lua function :D .
I've found it usefull for serialisation.
I hope y2.
There are some todos in "dump_var":

  • proper dumping strings ( "-> \" \ -> \\ )
  • proper dumping functions (As far as I know there is a posiblility for dumping functions)
function table.dump(t)
    local ret=nil ;
    local i,v = next(t,nil);
    local function dump_var(val)
        local ret 
        if( type(val) == 'number' ) then
            ret = tostring(val);
        elseif(type(val)== 'string' ) then
            ret = '"'..tostring(val)..'"';
            -- TODO substisution " -> \" and \ -> \\ 
        elseif(type(val)== 'table' ) then
            ret = table.dump(val)
            ret = '"'..tostring(val)..'"'
        return ret
    while i do
        if ret~=nil then ret=ret.."," else ret='' end
        i,v = next(t,i)
    if ret == nil then return '{ nil }' end
    return '{'..ret..'}'

Check it out:

t={[1]="one",[2]={[1]=1,[2]=2},[3]={[1]={["Re"]=1,["Im"]=6},["q"]=0,["p"]={["Re"]=5,["Im"]=3}},[4]="last",[0]="zero",["a"]={["b"]={["c"]={["d"]={["e"]={["f"]={["g"]="hijklmnop...."}}}}}},["fib"]="fibbonnacci test",[-1]="less then zero"}


O ile nie zaznaczono inaczej, treść tej strony objęta jest licencją Creative Commons Attribution-ShareAlike 3.0 License