Skip to content

Instantly share code, notes, and snippets.

@qizhihere
Created July 4, 2015 07:47
Show Gist options
  • Save qizhihere/cb2a14432d9bf65693ad to your computer and use it in GitHub Desktop.
Save qizhihere/cb2a14432d9bf65693ad to your computer and use it in GitHub Desktop.
merge two tables in lua
function table.merge(t1, t2)
for k,v in ipairs(t2) do
table.insert(t1, v)
end
return t1
end
@shaeinst
Copy link

shaeinst commented Apr 2, 2022

function merge_table(table1, table2)
	for _, value in ipairs(table2) do
		table1[#table1+1] = value
	end
	return table1
end

-- example
local table1 = {1, 2, 3}
local table2 = {4, 5, 6}

merged_table = merge_table(table1, table2)
for _, value in ipairs(merged_table) do
	print(value)
end

output
------------------
1
2
3
4
5
6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment