Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fuse array in Lua

Tags:

arrays

lua

how can I fuse two array into one to be like:

local array1 = {2272, 2271, 2270, 2269}
local array2 = {2267, 2266, 2268, 2265, 2264, 2263, 2262, 2261}
local fusedArray = {2272, 2271, 2270, 2269, 2267, 2266, 2268, 2265, 2264, 2263, 2262, 2261}

or

local array1 = {2292, 2291, 2290, 2289}
local array2 = {2267, 2266, 2268, 2265, 2264, 2263, 2262, 2261}
local fusedArray = {2292, 2291, 2290, 2289, 2267, 2266, 2268, 2265, 2264, 2263, 2262, 2261}
like image 833
whiteblXK Avatar asked May 03 '26 18:05

whiteblXK


2 Answers

The standard library can help with this:

local function concatArray(a, b)
  local result = {table.unpack(a)}
  table.move(b, 1, #b, #result + 1, result)
  return result
end

See table.move and table.unpack in the docs.

like image 166
luther Avatar answered May 06 '26 08:05

luther


You'll have to iterate both tables (using ipairs or pairs function) and insert elements into a third table. If you can modify one of them, then only iterate the other table and insert its elements into the first one.

like image 39
Paul Kulchenko Avatar answered May 06 '26 08:05

Paul Kulchenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!