Last active
December 20, 2015 03:59
-
-
Save obikag/6067395 to your computer and use it in GitHub Desktop.
Function to compare tables based on values within each table. Sequence of values in each table is not taken into consideration. This function returns true if all of the values in each of the tables match.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--Method to compare tables | |
function compare_tables(table1,table2) | |
temp = {} | |
if #table1 ~= #table2 then return false end | |
for _,v1 in pairs(table1) do | |
for _,v2 in pairs(table2) do | |
if v1 == v2 then | |
table.insert(temp,v1) | |
break | |
end | |
end | |
end | |
return #temp == (#table1 and #table2) | |
end | |
--Test Section | |
t1 = {1,3,2,6,10} | |
t2 = {10,2,6,1,3} --same as t1, diffrent sequence | |
t3 = {1,3,2,6,10} --same as t1 | |
t4 = {5,9,4,11,15} --different values | |
t5 = {6,8,8,6} --shorter table length | |
t6 = {1,3,5,6,10} --similar to t1, one digit changed | |
--Start test | |
print(compare_tables(t1,t2)) --true | |
print(compare_tables(t2,t3)) --true | |
print(compare_tables(t1,t3)) --true | |
print(compare_tables(t1,t4)) --false | |
print(compare_tables(t1,t5)) --false | |
print(compare_tables(t1,t6)) --false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment