Iterate through table using selected keys
snippet in lua

Iterate through table using selected keys

user1115

   local t = {
      {a1 = 11, a2 = 22, c = 3, d = 4},
      {a1 = 12, a2 = 23, c = 2, d = 4},
      {a1 = 13, a2 = 24, c = 1, d = 4},
      {a1 = 14, a2 = 25, c = 0, d = 4},
      {a1 = 15, a2 = 26, c = 0, d = 4}
      }
local keys_to_iterate = {"a1", "a2"}

for index = 1, #t do
  for k = 1, #keys_to_iterate do
    if t[index][keys_to_iterate[k]] then
      print(keys_to_iterate[k] , t[index][keys_to_iterate[k]])
    end
  end
end