Compare values of tables

Basically, I’m trying to compare the value of a table with another table and get the result. 

local alphabet = {"A","B","C","D","E","F","G"} local letters = {"A","C", "F"} local newLetters = {} for i=1,#letters do if letters[i] == alphabet[i] then newLetters[#newLetters +1] = alphabet[i] table.remove(letters, i) end end return newLetters

This only returns “A”. What’s wrong with this code?

Your code assumes that the index of letters and alphabet refer to the same values. You need a second for loop.

for i=1,#letters do for j = 1, #alphabet do if letters[i] == alphabet[j] then newLetters[#newLetters +1] = alphabet[j] -- table.remove(letters, i) -- you don't need to do this. end end end

The code is untested but something like this should work.

Rob

Your code assumes that the index of letters and alphabet refer to the same values. You need a second for loop.

for i=1,#letters do for j = 1, #alphabet do if letters[i] == alphabet[j] then newLetters[#newLetters +1] = alphabet[j] -- table.remove(letters, i) -- you don't need to do this. end end end

The code is untested but something like this should work.

Rob