Home > Net >  Loop for with the same key value(LUA)
Loop for with the same key value(LUA)

Time:04-16

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?

CodePudding user response:

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
  • Related