你是否允许在lua表中使用数字键?

时间:2013-04-29 21:12:59

标签: dictionary lua lua-table

以下示例应该创建一个表,它可以在数字和字符串之间进行转换,然后再返回,但无法运行。

是因为我在字典类型中使用数字键吗? 或者是因为lua从1开始表索引?

有没有更好的方法来实现这一目标?

dyeColor = {
    0="black"    ,  black     = 0,
    1="red"      ,  red       = 1, 
    2="green"    ,  green     = 2,
    3="brown"    ,  brown     = 3,
    4="blue"     ,  blue      = 4,
    5="purple"   ,  purple    = 5,
    6="cyan"     ,  cyan      = 6,
    7="lightGray",  lightGray = 7,
    8="gray"     ,  gray      = 8,
    9="pink"     ,  pink      = 9,
    10="lime"     ,  lime      =10,
    11="yellow"   ,  yellow    =11,
    12="lightBlue",  lightBlue =12,
    13="magenta"  ,  magenta   =13,
    14="orange"   ,  orange    =14,
    15="white"    ,  white     =15}

使用此在线翻译(http://repl.it/languages/Lua),它会给出错误

[string "stdin"]:2: '}' expected (to close '{' at line 1) near '='attempt to call a nil value

3 个答案:

答案 0 :(得分:6)

您需要将数字索引放在括号中:

dyeColor = {
    [0]="black"     ,  black     = 0,
    [1]="red"       ,  red       = 1, 
    [2]="green"     ,  green     = 2,
    [3]="brown"     ,  brown     = 3,
    [4]="blue"      ,  blue      = 4,
    [5]="purple"    ,  purple    = 5,
    [6]="cyan"      ,  cyan      = 6,
    [7]="lightGray" ,  lightGray = 7,
    [8]="gray"      ,  gray      = 8,
    [9]="pink"      ,  pink      = 9,
    [10]="lime"     ,  lime      =10,
    [11]="yellow"   ,  yellow    =11,
    [12]="lightBlue",  lightBlue =12,
    [13]="magenta"  ,  magenta   =13,
    [14]="orange"   ,  orange    =14,
    [15]="white"    ,  white     =15}

您可以使用以下方式为自己节省一些打字:

dyeColor = {
    [0]="black"     ,
    [1]="red"       ,
    [2]="green"     ,
    [3]="brown"     ,
    [4]="blue"      ,
    [5]="purple"    ,
    [6]="cyan"      ,
    [7]="lightGray" ,
    [8]="gray"      ,
    [9]="pink"      ,
    [10]="lime"     ,
    [11]="yellow"   ,
    [12]="lightBlue",
    [13]="magenta"  ,
    [14]="orange"   ,
    [15]="white"    }

for i = 0, #dyeColor do dyeColor[dyeColor[i]] = i end

Lua允许Name以[{1}}形式fieldspec,而不是数字Name = exp。数字必须放在括号中。现场参考也是如此。你可以说

dyeColor.black

但不是

dyeColor.0 -- you may say dyeColor[0] of course

答案 1 :(得分:0)

#dyeColor如果包含[0]则未定义,因此您可以

dyeColor = {} do
  local tmp= {
    "black"     ,
    "red"       ,
    "green"     ,
    "brown"     ,
    "blue"      ,
    "purple"    ,
    "cyan"      ,
    "lightGray" ,
    "gray"      ,
    "pink"      ,
    "lime"      ,
    "yellow"    ,
    "lightBlue" ,
    "magenta"   ,
    "orange"    ,
    "white"    
  }
  for i, v in ipairs(tmp) do
    local j = i - 1
    dyeColor[v], dyeColor[j] = j, v
  end
end

答案 2 :(得分:0)

改善Doug Curie的解决方案:

local dyeColor = {
    [0] = "black", -- You must set 0 explicitly
    "red"        , -- Unnamed items get their numeric index 
    "green"      , -- automatically, starting at 1.
    "brown"      ,
    "blue"       ,
    "purple"     ,
    "cyan"       ,
    "lightGray"  ,
    "gray"       ,
    "pink"       ,
    "lime"       ,
    "yellow"     ,
    "lightBlue"  ,
    "magenta"    ,
    "orange"     ,
    "white"
}
for i = 0, #dyeColor do dyeColor[dyeColor[i]] = i end

即使在Lua 5.2中未定义具有[0]的表的长度,它实际上返回最后一个元素的索引。上面的代码也适用于Lua 5.1和LuaJIT。