Jump to content

Comparing 2 tables with same values in different order


JeViCo

Recommended Posts

  • Moderators
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.

 

 

 

  • Like 1
Link to comment
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 by JeViCo
Link to comment
  • Moderators
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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...