在 LUA 表中查找值

发布于 2025-01-09 13:36:23 字数 898 浏览 0 评论 0原文

我在 lua g1、g2、g3 中有 3 个表,我正在寻找一种有效的方法来快速找到数字(ID)所在的表,

这就是我到目前为止所拥有的,它看起来效率低下。

g1 = {
    37863,
    78372,
    ...
    ...
}
g2 = {
    19599,
    84651,
    ...
    ...
}
g3 = {
    37462,
    42843,
    ...
    ...
}
for i = 1, 170000 do
    if (g1[i] == ID) then
        --number found in G1
        break
    elseif (g2[i] == ID) then
        --number found in G2
        break
    elseif (g3[i] == ID) then
        --number found in G3
        break
    end
end

我能想到的是,我可以重建表格,而不是将数字放在 g 表后面,将其直接放入表格中,如下所示:

g = {
    [37863] = 3,
    [78372] = 2,
    [18788] = 1,
}
if (g[ID] == 1) then
 --number found in G1
elseif (g[ID] == 2) then
 --number found in G2
elseif (g[ID] == 3) then
 --number found in G3
end

您认为可能最有效

I have 3 tables in lua g1, g2, g3 and I'm looking for an efficient way to quickly find which table a number (ID) is in

This is what I have so far and it seems inefficiently slow.

g1 = {
    37863,
    78372,
    ...
    ...
}
g2 = {
    19599,
    84651,
    ...
    ...
}
g3 = {
    37462,
    42843,
    ...
    ...
}
for i = 1, 170000 do
    if (g1[i] == ID) then
        --number found in G1
        break
    elseif (g2[i] == ID) then
        --number found in G2
        break
    elseif (g3[i] == ID) then
        --number found in G3
        break
    end
end

what I can think of is that I could reconstruct the tables, instead of putting the number after g table get it directly into the table like this:

g = {
    [37863] = 3,
    [78372] = 2,
    [18788] = 1,
}
if (g[ID] == 1) then
 --number found in G1
elseif (g[ID] == 2) then
 --number found in G2
elseif (g[ID] == 3) then
 --number found in G3
end

what you think might be most effective

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

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

发布评论

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

评论(2

聚集的泪 2025-01-16 13:36:23

您应该很容易在表上进行测试,无论如何您都应该这样做,但在一般情况下,第二个选项在大型表上进行随机访问会更快。如果您确实总是需要遍历整个列表并将其分成三个存储桶,那么第一个选项可能会更快,这就是为什么您需要在特定用例上测试它。

It should be easy for you to test on your tables, which is what you should do anyway, but in a general case the second option is going to be much faster on large tables for random access. If you do always need to go through the entire list and split it into three buckets, then the first option can be faster and that's why you need to test it on your specific use case.

幽梦紫曦~ 2025-01-16 13:36:23

如果你想收集一个特定的值,你可以简单地迭代你想要搜索的表,我可以给你一个函数来解释你的块。

local function find(table, value)
    for key, _value in pairs(table) do
        if type(_value) == 'table' then
            local f = { find(_value, value) }
            if #f ~= 0 then
                table.insert(f, 2, key); return unpack(f)
            end
        elseif _value == value or key == value then
            return key, _value
        end
    end
end

首先你要做的是创建函数,然后迭代它。扫描主可能有的所有子表,如果找到值:value,则返回获取该值的路径,否则不返回任何内容。

或者对于朋友来说:如果函数返回某些内容,则该值位于其中或某个子表中。

我希望这个结果对您有用,美好的一天!

If you want to collect a specific value you can simple iter the table where you want to search, I can give you a function explaining you the chuncks.

local function find(table, value)
    for key, _value in pairs(table) do
        if type(_value) == 'table' then
            local f = { find(_value, value) }
            if #f ~= 0 then
                table.insert(f, 2, key); return unpack(f)
            end
        elseif _value == value or key == value then
            return key, _value
        end
    end
end

First you do is create the function, and iter it. Scaning all sub-tables may the main table have, if the value: value is found return the path to get the value else return nothing.

Or for friends: if the function return something the value is in it or in some sub-table.

I hope this result useful to you, great day!

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