same
function same( arr, other ) --> res
Description
Checks if both array tables are the same. Array tables are the same if they have at each position the same values.
Parametes
- arr
-
An array table you want to compare.
- other
-
An array table you want to compare.
Return Values
- res
-
true if both array tables are the same, otherwise false.
Code
--ZFUNC-same-v1 local function same( tab, other ) --> res if type( tab ) ~= 'table' or type( other ) ~= 'table' then return tab == other end for k, v in pairs( tab ) do local otherVal = other[ k ] if otherVal == nil or not same( v, otherVal ) then return false end end for k, v in pairs( other ) do local tabVal = tab[ k ] if tabVal == nil or not same( v, tabVal ) then return false end end return true end return same
Examples
local t = require( "taptest" ) local same = require( "same" ) -- array t( same( { "a", "b", "c" }, { "a", "b", "c" } ), true ) -- different sizes t( same( { "a", "b" }, { "a", "b", "c" } ), false ) t( same( { "a", "b", "c" }, { "a", "c" } ), false ) -- different order t( same( { "a", "c", "b" }, { "a", "b", "c" } ), false ) -- mixed values t( same( { 13, false, "boom" }, { 13, false, "boom" } ), true ) -- dict local tab = { name = "Alexander", hobbies = { "movies", "boardgames", "programming" }, nicknames = { friends = "Sascha", parents = "Schurik" } } local other = { hobbies = { "movies", "boardgames", "programming" }, nicknames = { friends = "Sascha", parents = "Schurik" }, name = "Alexander" } t( same( tab, other ), true ) tab.hobbies = { "bowling", "dance" } t( same( tab, other ), false ) tab.hobbies = { "movies", "boardgames", "programming" } t( same( tab, other ), true ) other.name = "Sanja" t( same( tab, other ), false ) other.name = "Alexander" t( same( tab, other ), true ) t()