在多个Lua列表中搜索一个项目

发布于 2025-01-09 03:10:26 字数 266 浏览 0 评论 0原文

我现在正在编写一个脚本,遇到了一个障碍,我不太清楚如何检查某个值是否在 Lua 中的任何选定表中。

下面问题的示例脚本。

players = {213, 644}
helpers = {632, 965}

-- How would I check if a number (for example 632) was in either of these two tables?

I'm working on a script right now and have encountered a roadblock where I can't quite figure out how to check if a value is in any of the selected tables in Lua.

Example script of the problem below.

players = {213, 644}
helpers = {632, 965}

-- How would I check if a number (for example 632) was in either of these two tables?

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

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

发布评论

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

评论(3

眼中杀气 2025-01-16 03:10:26

使用这个简单的表查找值_v

local players = {213, 644}
local helpers = {632, 965}

local function find(t, _v)
    for i, v in pairs(t) do
        local type1 = type(v)
        if type1 == 'table' then
            local value = { find(v, _v) }
            if #value ~= 0 then
                table.insert(value, 2, i)
                return unpack(value)
            end
        elseif i == _v or v == _v then
            return i, v
        end
    end
end

-- Example:
print(find(players, 632) and 'Was finded in table: players') -- nil
print(find(helpers, 632) and 'Was finded in table: helpers') -- 'Was finded...'

Use this simple table find value _v:

local players = {213, 644}
local helpers = {632, 965}

local function find(t, _v)
    for i, v in pairs(t) do
        local type1 = type(v)
        if type1 == 'table' then
            local value = { find(v, _v) }
            if #value ~= 0 then
                table.insert(value, 2, i)
                return unpack(value)
            end
        elseif i == _v or v == _v then
            return i, v
        end
    end
end

-- Example:
print(find(players, 632) and 'Was finded in table: players') -- nil
print(find(helpers, 632) and 'Was finded in table: helpers') -- 'Was finded...'
命比纸薄 2025-01-16 03:10:26

要检查某个值是否在列表中,您需要遍历该列表并将每个元素与您要查找的值进行比较。
如果您想对多个列表执行此操作,则需要对每个列表执行此操作。

local lists = {players, helpers}
local foundIn = {}

for _, list in ipairs(lists) do
  for _, v in pairs(list) do
    if v == 632 then
      table.insert(foundIn, list)
      break
    end
  end
end

To check wether a value is in a list you need to traverse over that list and compare each element with the value you're looking for.
If you want to do this for multiple lists you need to do this for each of the lists.

local lists = {players, helpers}
local foundIn = {}

for _, list in ipairs(lists) do
  for _, v in pairs(list) do
    if v == 632 then
      table.insert(foundIn, list)
      break
    end
  end
end
一指流沙 2025-01-16 03:10:26

如果表是静态的或不经常更改,请尝试以下操作:

players = {213, 644}
helpers = {632, 965}
present = {}

for k,v in pairs(players) do present[v]=true end
for k,v in pairs(helpers) do present[v]=true end

print(present[632])
print(present[633])

如果您需要知道在哪里找到某个项目,请执行以下操作:

for k,v in pairs(players) do present[v]="players" end
for k,v in pairs(helpers) do present[v]="helpers" end

print(present[632])
print(present[644])

If the tables are static or do not change frequently, try this:

players = {213, 644}
helpers = {632, 965}
present = {}

for k,v in pairs(players) do present[v]=true end
for k,v in pairs(helpers) do present[v]=true end

print(present[632])
print(present[633])

If you need to know where an item was found, do this:

for k,v in pairs(players) do present[v]="players" end
for k,v in pairs(helpers) do present[v]="helpers" end

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