isarray

function isarray( tab ) --> res

Description

Checks if tab is an array table. If tab is not a table returns the function nil and an error message.

Parameters

tab

Table that should be checked.

Return Values

res

Is true if tab is an array table, otherwise false.

Code

--ZFUNC-isarray-v1
local function isarray( tab ) --> res
   if type( tab ) ~= "table" then return nil,"Argument is not a table! It is: "..type( tab ) end

   --check if all the table keys are numerical and count their number
   local count = 0
   for k, v in pairs( tab ) do
      if type( k ) == "number" then count = count + 1 else return false end
   end

   --all keys are numerical. now let's see if they are sequential and start with 1
   for i = 1, count do
      if not tab[ i ] and type( tab[ i ] ) ~= "nil" then return false end
   end

   return true
end

return isarray

Examples

local t = require( "taptest" )
local isarray = require( "isarray" )

t( isarray{ "a", "b", "c", "d" }, true )
t( isarray{ [ 1 ]="a", [ 2 ]="b", [ 3 ]="c", [ 4 ]="d" }, true )
t( isarray{ [ 1 ]="a", [ 2 ]="b", [ 3 ]="c", [ 5 ]="d" }, true )
t( isarray{ [ 1 ]="a", [ 2 ]="b", [ 3 ]="c", [ "4" ]="d" }, false )

t()

Inspired by

See also