Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Lua: unpack multiple tables (See related posts)

The standard unpack function only operates on one table. The following snippet will unpack elements from any number of tables.

---
-- Returns all elements from all table arguments
function unpacks( ... )
  local values = {}

  -- Collect values from all tables
  for i = 1, select( '#', ... ) do
    for _, value in ipairs( select( i, ... ) ) do
      values[ #values + 1] = value
    end
  end

  return unpack( values )
end

x = {'a', 'b', 'c', 'd', 'e'}
y = {'v', 'w', 'x', 'y', 'z'}
print( unpacks( x, y ) )


You need to create an account or log in to post comments to this site.


Click here to browse all 5140 code snippets

Related Posts