insertall

function insertall( arr, [pos,] oth ) --> arr

Description

Inserts all values of oth at position pos in arr. The default value for pos is #arr+1, so that a call insertall( arr, oth ) inserts all values at the end of arr.

Parameters

arr

Array table where the values should be inserted.

pos

Position in the array table where the values should be inserted. The default value is #arr+1.

oth

Array table with the values that should be inserted.

Return Values

arr

Returns the modified table arr.

Code

--ZFUNC-insertall-v1
local function insertall( arr, pos, oth ) --> arr
   if not oth then
      oth = pos
      pos = #oth + 1
   end

   for _, v in ipairs( oth ) do
      table.insert( arr, pos, v )
      pos = pos + 1
   end

   return arr
end

return insertall

Examples

local t = require( "taptest" )
local insertall = require( "insertall" )
local same = require( "same" )

arr = insertall( { "a", "b" }, { "c", "d" } )
t( same( arr, { "a", "b", "c", "d" } ), true )

arr = { 1, 2, 5 }
insertall( arr, 3, { 3, 4 } )
t( same( arr, { 1, 2, 3, 4, 5 } ), true )

t()