Lua在我将标识符放入表时将其转换为字符串?

Lua在我将标识符放入表时将其转换为字符串?,lua,identifier,Lua,Identifier,如果我使用a.b,Lua是否将b识别为字符串而不是标识符?来自Lua文档: 初学者的一个常见错误是将A.x与[x]混淆。第一种形式表示[“x”],即由字符串“x”索引的表。第二种形式是由变量x的值索引的表 如果有人想解释Lua为什么这样做,请随意做!然而,这个问题在这一点上得到了回答 UPD在for循环之前,a={['b']='white',['s']=2}忘记键集表;这并不能转化任何东西 a = {} b = "s" a.b = "white" a[&q

如果我使用a.b,Lua是否将b识别为字符串而不是标识符?

来自Lua文档:

初学者的一个常见错误是将A.x与[x]混淆。第一种形式表示[“x”],即由字符串“x”索引的表。第二种形式是由变量x的值索引的表

如果有人想解释Lua为什么这样做,请随意做!然而,这个问题在这一点上得到了回答


UPD在for循环之前,
a={['b']='white',['s']=2}

忘记键集表;这并不能转化任何东西

a = {}
b = "s"
a.b = "white"
a["s"] = 2
local keyset={}
local n=0

for k,v in pairs(a) do
    n=n+1
    keyset[n]=k
    print(type(k))
-- output is String(x2)
end
键:s字符串值:2个数字
键:b字符串val:白色字符串

s 2

不是很明显吗?不是。在某些语言中,你必须明确地告诉程序什么是字符串,以便它毫无例外地使用引号将某个东西解析为字符串。这是我第一次看到这个,所以一开始我很困惑。
#! /usr/bin/env lua

a = {}
b = "s"  --  completely different variable, not used in a

a.b = "white"  --  syntactic sugar for  a["b"] = "white"
a["s"] = 2  --  variable b just happens to have same value as this key, so it fits

--  at this point there are two entries in a
--  a["b"] = "white"   and   a["s"] = 2

for k, v in pairs( a ) do
    print( 'key:', k, type(k), '  val:', v, type(v) )
end

print( b, a[b] )  --  retrieve value from  a["s"]  coincidental key fit