【问题标题】:Lua Multidimensional Table CreateLua 多维表创建
【发布时间】:2011-08-07 03:55:24
【问题描述】:

我在 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 },

}

错误:'<name>' expect near ':'

【问题讨论】:

    标签: arrays lua lua-table


    【解决方案1】:

    ::= 有点奇怪。看起来你在做 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
    

    【讨论】:

      【解决方案2】:

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

      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 ###
      

      【讨论】:

        猜你喜欢
        • 2017-05-10
        • 2021-02-25
        • 2019-10-23
        • 2017-02-19
        • 2014-06-18
        • 2021-07-03
        • 1970-01-01
        • 2014-12-10
        • 2012-11-07
        相关资源
        最近更新 更多