Lua多维表创建

发布于 2024-11-02 10:25:01 字数 946 浏览 0 评论 0原文

我在 Lua 中有一个多维表,但我似乎无法创建它以便在 Lua 中使用?

桌子

items ::= {

        {["category"]="tools", ["name"]="hammer", ["price"]=10,  ["quantity"]=5 },
        {["category"]="tools", ["name"]="saw", ["price"]=15,  ["quantity"]=4 },
        {["category"]="tools", ["name"]="screwdriver", ["price"]=4,  ["quantity"]=12 },
        {["category"]="tools", ["name"]="measuring tape", ["price"]=9,  ["quantity"]=3 },
        {["category"]="tools", ["name"]="pliers", ["price"]=10,  ["quantity"]=5 },
        {["category"]="tools", ["name"]="wrench", ["price"]=10,  ["quantity"]=5 },


        {["category"]="fasteners", ["name"]="nails", ["price"]=.1,  ["quantity"]=1500 },
        {["category"]="fasteners", ["name"]="screws", ["price"]=.2,  ["quantity"]=1200 },
        {["category"]="fasteners", ["name"]="staples", ["price"]=.05,  ["quantity"]=2000 },

}

错误:<代码>'<名称>'期望靠近“:”

I have a Multidimensional table in Lua but I can't seem to create it to be able to use in Lua?

Table

items ::= {

        {["category"]="tools", ["name"]="hammer", ["price"]=10,  ["quantity"]=5 },
        {["category"]="tools", ["name"]="saw", ["price"]=15,  ["quantity"]=4 },
        {["category"]="tools", ["name"]="screwdriver", ["price"]=4,  ["quantity"]=12 },
        {["category"]="tools", ["name"]="measuring tape", ["price"]=9,  ["quantity"]=3 },
        {["category"]="tools", ["name"]="pliers", ["price"]=10,  ["quantity"]=5 },
        {["category"]="tools", ["name"]="wrench", ["price"]=10,  ["quantity"]=5 },


        {["category"]="fasteners", ["name"]="nails", ["price"]=.1,  ["quantity"]=1500 },
        {["category"]="fasteners", ["name"]="screws", ["price"]=.2,  ["quantity"]=1200 },
        {["category"]="fasteners", ["name"]="staples", ["price"]=.05,  ["quantity"]=2000 },

}

Error: '<name>' expect near ':'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄暮涼年 2024-11-09 10:25:01

::= 有点奇怪。看起来你正在做 ASN.1 而不是 Lua 。

试试这个:

items = {

        {["category"]="tools", ["name"]="hammer", ["price"]=10,  ["quantity"]=5 },
        {["category"]="tools", ["name"]="saw", ["price"]=15,  ["quantity"]=4 },
        {["category"]="tools", ["name"]="screwdriver", ["price"]=4,  ["quantity"]=12 },
        {["category"]="tools", ["name"]="measuring tape", ["price"]=9,  ["quantity"]=3 },
        {["category"]="tools", ["name"]="pliers", ["price"]=10,  ["quantity"]=5 },
        {["category"]="tools", ["name"]="wrench", ["price"]=10,  ["quantity"]=5 },


        {["category"]="fasteners", ["name"]="nails", ["price"]=.1,  ["quantity"]=1500 },
        {["category"]="fasteners", ["name"]="screws", ["price"]=.2,  ["quantity"]=1200 },
        {["category"]="fasteners", ["name"]="staples", ["price"]=.05,  ["quantity"]=2000 },

}

当我使用它时,我在 Lua shell 中得到以下内容:

for k,v in pairs(items) do for k1,v1 in pairs(v) do print(k1,v1) end end
price   10
quantity    5
name    hammer
category    tools
price   15
quantity    4
name    saw
category    tools
price   4
quantity    12
name    screwdriver
category    tools
price   9
quantity    3
name    measuring tape
c    ategory    tools
price   10
quantity    5
name    pliers
category    tools
price   10
quantity    5
name    wrench
category    tools
price   0.1
quantity    1500
name    nails
category    fasteners
price   0.2
quantity    1200
name    screws
category    fasteners
price   0.05
quantity    2000
name    staples
category    fasteners

That ::= is a bit bizarre. It looks like you're doing ASN.1 instead of Lua there.

Try this instead:

items = {

        {["category"]="tools", ["name"]="hammer", ["price"]=10,  ["quantity"]=5 },
        {["category"]="tools", ["name"]="saw", ["price"]=15,  ["quantity"]=4 },
        {["category"]="tools", ["name"]="screwdriver", ["price"]=4,  ["quantity"]=12 },
        {["category"]="tools", ["name"]="measuring tape", ["price"]=9,  ["quantity"]=3 },
        {["category"]="tools", ["name"]="pliers", ["price"]=10,  ["quantity"]=5 },
        {["category"]="tools", ["name"]="wrench", ["price"]=10,  ["quantity"]=5 },


        {["category"]="fasteners", ["name"]="nails", ["price"]=.1,  ["quantity"]=1500 },
        {["category"]="fasteners", ["name"]="screws", ["price"]=.2,  ["quantity"]=1200 },
        {["category"]="fasteners", ["name"]="staples", ["price"]=.05,  ["quantity"]=2000 },

}

When I use that, I get the following in the Lua shell:

for k,v in pairs(items) do for k1,v1 in pairs(v) do print(k1,v1) end end
price   10
quantity    5
name    hammer
category    tools
price   15
quantity    4
name    saw
category    tools
price   4
quantity    12
name    screwdriver
category    tools
price   9
quantity    3
name    measuring tape
c    ategory    tools
price   10
quantity    5
name    pliers
category    tools
price   10
quantity    5
name    wrench
category    tools
price   0.1
quantity    1500
name    nails
category    fasteners
price   0.2
quantity    1200
name    screws
category    fasteners
price   0.05
quantity    2000
name    staples
category    fasteners
时光礼记 2024-11-09 10:25:01

这是创建表格的一种方法。

function create_table()
    local l={} -- Initialize table.
    l[0]=[[]] -- Clear nil @ index 0.
    return l
end
t=create_table()
print(t) -- Prints table which is stored in memory.
--[[
    If you don't want to create everyday tables, you can use this matrix.
]]--
m={{["id"]=1},{["id"]=2,["name"]="Mr. Anon",["leet"]=1337}}
print(m[1]["id"]..", "..m[2]["id"]..", "..m[2]["name"]..", "..m[2]["leet"]..".")


# Table creation #
## Introduction ##
### How to guide ###

Here's a way you could create tables.

function create_table()
    local l={} -- Initialize table.
    l[0]=[[]] -- Clear nil @ index 0.
    return l
end
t=create_table()
print(t) -- Prints table which is stored in memory.
--[[
    If you don't want to create everyday tables, you can use this matrix.
]]--
m={{["id"]=1},{["id"]=2,["name"]="Mr. Anon",["leet"]=1337}}
print(m[1]["id"]..", "..m[2]["id"]..", "..m[2]["name"]..", "..m[2]["leet"]..".")


# Table creation #
## Introduction ##
### How to guide ###
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文