If table contains a key with specific value

lua, lua-table

Solution

You can loop through the table and check:

for i, v in ipairs(Ant) do
  if v.age == 3 then
    print( i )
  end
end

It'll print the index at which your 3 year old ants are stored.

Problem

It might be a bit confusing, but I have a table, for example, called `Ant`. This table, contains a bunch of other (unnamed) tables. These tables represent ants, and hold values. Kind of like this: ``` Ant = { {age=3,speed=10}, {age=6,speed=7} } ``` My question is, how would I check if any of the unnamed table inside of the Ant table contains a specific value to `age`. So, for example, I'd like to check if any of my ants are aged 3 years old. I hope I was clear enough, and thanks in advance!

Original source