Table Comparison
Posted: Sun Jun 14, 2015 10:00 am
Hey all,
This is more of a Lua question than LOVE. However, I am trying to compare tables, and have tried a few different ways, each with varying levels of success. End result, I want to check that table qa.entity.materials has all the required values of qa.ship.materials, but also if entity has the material[1] it also checks the second value in the table to make sure the amount in qa.entity.materials[2] is higher. I'll show you what I have here:
This is a hardcode version for testing, copy paste here to see what I mean.
It looks like it works at this point, but if you change qa.entity.materials to {{10, 50}}, it still passes. Since one value did match, even though the remaining tables in qa.ship.materials has unchecked values. I checked some more advanced table comparison topics(here), but I cant seem to come to a solution that works well. Is there a way to check in either for loop that if the value in qa.ship.materials[1] does not match one within qa.entity.materials, if returns false but if true continues checking? Let me know if clarification is needed, but I could really use some help, on what I assume to be a trivial issue lol
Edit>> Forgot to mention, I did come up with a gritty way of doing it and that was by assigning another value to the array so it would not be nil, then looping through the list one more time, and returning false is any of the [3] were nil. But this way seems like the long way
This is more of a Lua question than LOVE. However, I am trying to compare tables, and have tried a few different ways, each with varying levels of success. End result, I want to check that table qa.entity.materials has all the required values of qa.ship.materials, but also if entity has the material[1] it also checks the second value in the table to make sure the amount in qa.entity.materials[2] is higher. I'll show you what I have here:
This is a hardcode version for testing, copy paste here to see what I mean.
Code: Select all
local qa = {ship = {}, entity = {}}
qa.ship.materials = {{10, 50}, {25, 100}, {95, 22}}
qa.entity.materials = {{10, 50}, {25, 100}, {95, 22}}
function qa.checkMaterials()
for i=1,#qa.ship.materials do
for _,v in pairs(qa.entity.materials) do
if qa.ship.materials[i][1] == v[1] then
if qa.ship.materials[i][2] > v[2] then
return false
else
print("Passed")
end
end
end
end
return true
end
if qa.checkMaterials() then print("All Clear") else print("Failed") end
Edit>> Forgot to mention, I did come up with a gritty way of doing it and that was by assigning another value to the array so it would not be nil, then looping through the list one more time, and returning false is any of the [3] were nil. But this way seems like the long way