title: Checking for items in tables Lua
body: <p>I have an input file</p>
  
  <pre><code>Corn Fiber 17
  Beans Protein 12
  Milk Protien 15
  Butter Fat 201
  Eggs Fat 2
  Bread Fiber 12
  Eggs Cholesterol 4
  Eggs Protein 8
  Milk Fat 5
  </code></pre>
  
  <p>This is loaded into a table. I can then run commands to check the value of the items. For example <code>&gt;print(foods.Eggs.Fat)
  2</code></p>
  
  <p>What I need to do is be able to search if an item is already in the table. I have a function that checks if the table has a value, but it doesn't seem to be working. My code:</p>
  
  <pre><code>  file = io.open("food.txt")
  
      function has_value (tab, val)
      for index, value in ipairs(tab) do
          if value == val then
              return true
          else
              return false
          end
      end
  end
  
      foods = {}
      for line in file:lines() 
          do
              local f, n, v = line:match("(%a+) (%a+) (%d+)")
              if foods[f] then
                  foods[f][n] = v
              else
                  foods[f] = {[n] = v}
              end
          end
      file:close()
  
      if has_value(foods, "Eggs") then
          print("Yes")
      else
          print("No")
      end
  </code></pre>
  
  <p>Even if the table does contain the item, I still am getting false returned from the function. In the example above, <code>if has_value(names, "Eggs") then</code> is printing "No" even when I know Eggs is in the table. Where is my error?</p>