Q:

How to run table and print a specific key

I have a table:
employee = {
             name = "John", age = 30,
             name = "George", age = 35
           }

Now, I want to run the whole table, and if the name "George" is found, then the appropriate age to be printed. How can I do this? From what I searched I found that you can run a list with the code for k, v in pairs(employee) do but I don't know how to continue from there.

A:

Your table will need to be restructured to start off with.
employee = {
    {name = "John", age = 30},
    {name = "George", age = 35},
}

This will allow the age to always match up with the name. Then you could run:
for _,v in ipairs(employee) do
    if v.name == "George" then
        return v.age
    end
end

You can even create it as a function so you can check whatever name you want:
function find_age(n)
  for _,v in ipairs(employee) do
      if v.name == n then
          return v.age
      end
  end
end

Of course, this will return the age of every n in the table, though.
Updated for comment
You can do it with a function as well.
function add_employee(n,a)
  table.insert(employee, {name = n, age = a})
end

