从字符串索引的表中获取值

时间:2017-10-13 22:30:57

标签: indexing lua lua-table

我有LUA表:

flys = {
["Moltres"] = {229, 2300}, -- moltres
["Articuno"] = {230, 2100}, -- artic
["Zapdos"] = {224, 2600}, -- zapdos
["Mew"] = {232, 2200}, -- 1000
["Mewtwo"] = {233, 2200},-- two
["Dragonite"] = {221, 1300},-- nite
["Pidgeot"] = {222, 900}, -- geot
["Fearow"] = {226, 800}, -- fearow
["Aerodactyl"] = {227, 1100}, -- aero
["Charizard"] = {216, 1000}, -- chari
["Porygon"] = {316, 600}, -- porygon
["Shiny Dragonite"] = {1020, 1300},-- Shiny nite
["Shiny Pidgeot"] = {996, 900}, -- Shiny geot
["Shiny Fearow"] = {997, 800}, -- Shiny fearow          --alterado v2.5
["Shiny Charizard"] = {295, 1000}, -- Shiny chari
["Porygon2"] = {648, 890}, -- 2
["Skarmory"] = {649, 1000}, -- skarmory
["Crobat"] = {652, 1190}, -- crobat

["Dragonair"] = {1112, 1150},
["Shiny Dragonair"] = {1113, 1400},  --alterado v2.5
["Noctowl"] = {994, 1000},
}

它由字符串索引,每个元素包含2个数字。

我无法获得价值观。我想例如从一些表对象中打印数字。

最明显的方式(对我来说)不起作用:

local pokemon3 = flys["Pidgeot"]
print("pokemon id: "..pokemon3[1])

它表示零值。我尝试了很多这些组合而没有工作。

1 个答案:

答案 0 :(得分:3)

我没有看到脚本的任何问题,它对我来说很好(在Lua 5.1,5.2和5.3中测试)。

您展示的方式是正确的方法,如果您仍有问题,则需要发布不适合您的完全脚本以及环境的详细信息正在尝试运行它。