Lua: merge two tables, awesome3-rc2 config

Awesome-rc is out, so have upgraded and fixed my config accordingly.
Whilst doing so, I needed a lua function to add all the elements of one table to another, similar to python’s list.extend() method. There isn’t one built into lua, so I rolled my own (with some help from #lua).

function joinTables(t1, t2)
		for k,v in ipairs(t2) do table.insert(t1, v) end return t1
end

et voilĂ 
My most recent awesome config can be found, as ever, here

About these ads

One thought on “Lua: merge two tables, awesome3-rc2 config

  1. That will have some consequences if the value is a table (insde of table to copy)… As tables are ids, when you change a table from inside of t2 the table in t1 is affected as well :(

    :

    local t1 = {“epic fail”, 3434, {“LOL”}, “hihihi”}
    local t2 = {}

    function joinTables(t1, t2)
    for k,v in ipairs(t2) do table.insert(t1, v) end return t1
    end

    joinTables(t2, t1)

    t2[3][1] = “HIO”

    print(t1[3][1])

    Will print “HIO”, even though it was t2 i edited not t1 :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s