Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua Multidimensional Table Create

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 ':'

like image 732
Steven Avatar asked Feb 13 '26 06:02

Steven


2 Answers

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
like image 56
JUST MY correct OPINION Avatar answered Feb 14 '26 20:02

JUST MY correct OPINION


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 ###
like image 29
Anon Ops Avatar answered Feb 14 '26 21:02

Anon Ops



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!