JeViCo Posted November 5, 2018 Share Posted November 5, 2018 Sup guys I have 2 tables with same values but they're in different order -. I'm looking for better method to compare this 2 tables without losing performance Link to comment
Gordon_G Posted November 5, 2018 Share Posted November 5, 2018 What do you mean by compare ? Link to comment
Moderators IIYAMA Posted November 5, 2018 Moderators Share Posted November 5, 2018 2 hours ago, JeViCo said: Sup guys I have 2 tables with same values but they're in different order -. I'm looking for better method to compare this 2 tables without losing performance The most fast methods are very dirty. I will only explain those ones when I think you have learned enough from the clean ones. But make one first. Just two loops, one nested in to the other. And try to figure out what the most logic human method would be to achieve your goal. If it fails, don't worry, just post what you tried. But it has to be yours, only then I will help you. 1 Link to comment
JeViCo Posted November 9, 2018 Author Share Posted November 9, 2018 (edited) local table1 = {"1", "2", "3"} local table2 = {"2", "3", "1"} function sameTables(tab1, tab2) local endTable = {} if #tab1 == #tab2 then for _, v in ipairs(tab1) do local check for _,v2 in ipairs(tab2) do if v2 == v then check = true break end end if not check then break return false end end else return false end end @IIYAMA can i write 'break' twice to stop both loops? Edited November 9, 2018 by JeViCo Link to comment
Moderators IIYAMA Posted November 9, 2018 Moderators Share Posted November 9, 2018 1 hour ago, JeViCo said: local table1 = {"1", "2", "3"} local table2 = {"2", "3", "1"} function sameTables(tab1, tab2) local endTable = {} if #tab1 == #tab2 then for _, v in ipairs(tab1) do local check for _,v2 in ipairs(tab2) do if v2 == v then check = true break end end if not check then break return false end end else return false end end @IIYAMA can i write 'break' twice to stop both loops? If the break keyword is used directly inside the loop you want to stop, then yes. ---- -- works for ... do for ... do break end break end ---- ---- -- does not work for ... do for ... do break break end end ---- If your code works, then first run a speed test. (So you can actually see if your speed improves with every action you take. local timeNow = getTickCount() for speedTestIndex=1, 1000 do -- <<< put your code in here end local endTime = getTickCount() outputChatBox("code duration: " .. (endTime - timeNow)) Write down below your code how long this took. (It is best to run the test twice per code) Improvement your code: - use the basic for loop, instead of the ipairs loop and test again. (The same type as the one of the speed test) Note: this code does not test for double items, is that a must? Link to comment
Gordon_G Posted November 9, 2018 Share Posted November 9, 2018 function table.contains(t, d) for _, v in pairs(t) do if v == d then return true end end return false end You could also use this function (I have an entire code for table comparing with this code so it can work with it) Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now