lua嵌套以进行循环和表格。插入创建多个条目

发布于 2025-02-09 23:21:51 字数 1393 浏览 2 评论 0原文

仍在这里学习,因此很可能是实现我的目标的一种不太复杂的方法。这是我从代码中撤出的片段,该代码可按预期工作,但我的最后一步失败了。

    inv = {}
    
    for i = 23,mq.TLO.Me.NumBagSlots()+22 do
        inv[i] = {}
        for j = 1,mq.TLO.Me.Inventory(i).Container() do
            inv[i][j]={Item=mq.TLO.Me.Inventory(i).Item(j),bag=i,slot=j}
        end
    end
    
    local sortthatsucker={}
    for _,bags in pairs(inv) do
        for _,invcontainer in pairs(bags) do
            for _,_ in pairs(invcontainer) do
                local compactstring = tostring(invcontainer.Item).."-"..tostring(invcontainer.bag).."_"..tostring(invcontainer.slot)
                if tostring(invcontainer.Item) ~= "NULL" then
                    table.insert(sortthatsucker,compactstring)
                end
            end
        end
    end
    
    table.sort(sortthatsucker)
    for k,v in pairs(sortthatsucker) do
        print(k,"-",v)
    end

当前将提取,过滤和对所有项目进行提取,如果您可以按名称进行排序的简化k,v表,则创建一个复合字符串,显示项目名称和当前“坐标”,但由于我相信3个嵌套了循环以将数据提取出来,table.insert将在最后一个表中创建三个相同的条目,尽管每个条目具有唯一的键值。

IE:

163-shovel-23_10
164-shovel-23_10
165-shovel-23_10
166-tonsils-18_5
167-tonsils-18_5
168-tonsils-18_5
169-withers-6_12
170-withers-6_12
171-withers-6_12

等...

我的目标是为每个找到的每个项目都有一个表格,并为循环做一组新的嵌套以将x和y值从该字符串中拉出,以与该项目相互作用。协调并将其移至新的X和Y。如果有一种方法可以创建初始表并与之互动,而是将其作为嵌套表以获得相同的结果,那也将是有益的,因为我无法在尝试和尝试两周后弄清楚这一点搜索。

Still learning here, so most likely there's a less complicated way to achieve my goals. This is a snippet I pulled from my code that 90% works as intended, but I'm failing on my last step.

    inv = {}
    
    for i = 23,mq.TLO.Me.NumBagSlots()+22 do
        inv[i] = {}
        for j = 1,mq.TLO.Me.Inventory(i).Container() do
            inv[i][j]={Item=mq.TLO.Me.Inventory(i).Item(j),bag=i,slot=j}
        end
    end
    
    local sortthatsucker={}
    for _,bags in pairs(inv) do
        for _,invcontainer in pairs(bags) do
            for _,_ in pairs(invcontainer) do
                local compactstring = tostring(invcontainer.Item).."-"..tostring(invcontainer.bag).."_"..tostring(invcontainer.slot)
                if tostring(invcontainer.Item) ~= "NULL" then
                    table.insert(sortthatsucker,compactstring)
                end
            end
        end
    end
    
    table.sort(sortthatsucker)
    for k,v in pairs(sortthatsucker) do
        print(k,"-",v)
    end

Will currently extract, filter, and sort all items as I want, creating a compound string showing the item name and current "coords" if you will for simplified k,v table that's sortable by name, but due to I believe the 3 nested for loops to pull the data out, the table.insert will create three identical entries in the final table, albeit with unique key values for each entry.

ie:

163-shovel-23_10
164-shovel-23_10
165-shovel-23_10
166-tonsils-18_5
167-tonsils-18_5
168-tonsils-18_5
169-withers-6_12
170-withers-6_12
171-withers-6_12

etc...

My goal is to have this table with a single entry for each item found, and do a set of new nested for loops to pull the x and y values out of that string to interact with that item at that give coordinate and move it to a new x and y. If there was a way to create the initial table and interact with that, rather make it as a nested table to achieve the same result, that would also be beneficial, as I have not been able to figure that out after two weeks of trying and searching.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文