Print does not return table values

3
pokecatches = {
["Bulbasaur"] = {chance = 150, corpse = 5969},
["Ivysaur"] = {chance = 275, corpse = 5982},
["Venusaur"] = {chance = 400, corpse = 5962},
}
print(table.maxn(pokecatches))
corpses = {}
for x=1, table.maxn(pokecatches) do
table.insert(corpses, pokecatches[x].corpse)
end
print(table.concat(corpses, ","))
io.read()

This section should add the corpse value to the corpses table, but it will not, print (table.man (pokecatches)) returns 0. Does anyone know why?

    
asked by anonymous 03.09.2014 / 04:24

1 answer

4

The problem is that you want to iterate in a set as if it were a simple table.

One possibility of adjusting the code is to restructure the data as a table, to iterate by numeric index ( pokecatches[x]) :

pokecatches = {
   { name = "Bulbasaur", chance = 150, corpse = 5969},
   { name = "Ivysaur"  , chance = 275, corpse = 5982},
   { name = "Venusaur" , chance = 400, corpse = 5962},
}
print(table.maxn(pokecatches))
corpses = {}
for x=1, table.maxn(pokecatches) do
   table.insert(corpses, pokecatches[x].corpse)
end
print(table.concat(corpses, ","))

Outram to maintain the use of sets , iterates with for .. in to get the key and value data with pair :

pokecatches = {
   ["Bulbasaur"] = {chance = 150, corpse = 5969},
   ["Ivysaur"] = {chance = 275, corpse = 5982},
   ["Venusaur"] = {chance = 400, corpse = 5962},
}

corpses = {}
for name, data in pairs(pokecatches) do
   table.insert(corpses, data.corpse)
end
print(table.concat(corpses, ","))
  

See the two working on IDEONE

    
03.09.2014 / 04:37