'Loop for with the same key value(LUA)

local config = {
     ['dog'] = {amount = 500},
     ['dog'] = {amount = 600},
     ['dog'] = {amount = 700},
}

for k,v in pairs(config) do
    print(v)
end
Output: table:0x244b890 - (500)

How to output all values from the table? Is it possible with the same keys?



Solution 1:[1]

I don't think it's possible, because in Lua tables, the keys are unique. So, I am afraid you have to use another way to represent your data.

local config = {
    { 'dog', amount = 500 },
    { 'dog', amount = 600 },
    { 'dog', amount = 700 }
}

for Index = 1, #config do
  print(config[Index].amount)
end

The result will be:

500
600
700

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Robert