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( "tapered" )
local insertall = require( "insertall" )

t.same( { "a", "b", "c", "d" }, insertall( { "a", "b" }, { "c", "d" } ) )

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

t.done()