cutarr

function cutarr( arr, n ) --> left, right

Description

The function splits an array table at position n in two pieces. The element at n will be part of the left sub array table.

Parameters

arr

The array table that should be split.

n

The position in the array table where the cut happens.

Return Values

left

The left part of the array table.

right

The right part of the array table.

Code

--ZFUNC-cutarr-v1
local function cutarr( arr, n ) --> left, right
   local left = {}
   for i = 1, n do table.insert( left, arr[ i ] ) end
   local right = {}
   for i = n+1, #arr do table.insert( right, arr[ i ] ) end
   return left, right
end

return cutarr

Examples

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

left, right = cutarr( { "a", "b", "c" }, 2 )
t( same( left, { "a", "b" } ), true )
t( same( right, { "c" } ), true )

t()