How to find an ordered pair in a table

i asked chatgpt but it no good

it told me to

-- Sample table data
local myTable = {
  {1, 2}, -- This is the value you're looking for
  {3, 4},
  {5, 6},
}

-- Value to search for
local valueToFind = {1, 2}

-- Check if the value exists in the table
local foundIndex = table.find(myTable, valueToFind)

if foundIndex then
  print("Value found at index:", foundIndex)
else
  print("Value not found in the table.")
end

Pretty sure that doesn’t work because the table {1, 2} inside myTable isn’t the same as the valueToFind table. When determining if two tables are equal, I am pretty sure lua compares the memory location of the tables rather than the content

You can either make your own table.find function that compares the content of the tables, or do whatever you are trying to achieve differently

I’m confused at what you’re trying to achieve here? You want to compare the 2 tables to find if they have matching values?

local myTable = {
	{1, 2},
	{3, 4},
	{5, 6},
}


local valueToFind = {1, 3}

local foundIndex = false
for i, v in pairs(myTable) do
	if v[1] == valueToFind[1] and v[2] == valueToFind[2] then 
		foundIndex = true
	end
end

if foundIndex then
	print("Value found at index:", foundIndex)
else
	print("Value not found in the table.")
end

This seems to work. If that’s what you were trying to achieve that is.

1 Like

I made a mistake, I’ve edited it. The code should work now. That will print value not found in the table, to make it print true you need to set valueToFind = {1, 2}. Let me know if that works for you.