给一个变量赋一个数值和字符串值?

我试图创建一个变量,既包含数字值又包含字符串值。

我正在编写 Lua 代码,不知道如何做到这一点。这是可能的吗?

点赞
用户501459
用户501459

表格

它们就像是一个文件柜,在其中可以存储任意数量的值,并在给定某种“键”的情况下检索它们。在 Lua 中,键可以是任何类型,但最常见的键将是数值索引或字符串。

假设有:

local age = 30  -- 你的数值
local name = 'Fred' -- 你的字符串

在 Lua 中,有很多不同的组织方式:

local person = { age = 30, name = 'Fred' )
print(person.age, person.name)

local person = { 'Fred', 30 }
print(person[1], person[2])
print(unpack(person))

local person = { Fred = 30 }
print(person.Fred)

local person = { [30] = 'Fred' }
print(person[30])

等等。

2013-02-06 04:46:30
用户1984712
用户1984712

如果我使用..

coal = { name = "煤", value = 80 }

那么我是否可以这样做?

    userInput = read()

    if userInput == coal.name then
        fuelUse = coal.value
    end
2013-02-06 16:33:08