wolf391
wolf391

Reputation: 11

Filling a table with key,value pairs

I want to insert key,value pairs into a table

my_table = {}
for i=1, GetNumGroupMembers() do
    local unitID = "group"..i

    my_table.unitID = UnitName(unitID)
end

for key,value in pairs(my_table) do print(key,value) end

RESULT:

unitID group1

why is the key always "unitID" ? I need the VALUE of unitID as key, not the variable name

Upvotes: 1

Views: 443

Answers (1)

Robin Gertenbach
Robin Gertenbach

Reputation: 10776

The key is always unitID because that's what you call it literally when doing

my_table.unitID = UnitName(unitID)

what you want to do is

my_table[unitID] = UnitName(unitID)

Which will use the value of the variable unitID as the key.

Upvotes: 5

Related Questions