循环直到在表中找到 2 个特定值?

2024-04-22

我试图找到一种更聪明的方法来解决这个问题。

这是与游戏相关的代码的摘录,它循环遍历每个背包的每个插槽,直到找到铲子和绳子

local continue
local foundShovel, foundRope
        for i = 0, Container.GetLast():Index() do -- looping trough backpacks
        local cont = Container(i)
            for j = 0, cont:ItemCount()-1 do -- looping trough each slot
            local id = cont:GetItemData(j).id -- Getting ID of that slot
            foundShovel, foundRope = GetToolIndex(id,0) or foundShovel,GetToolIndex(id,1) or foundRope -- confusing...
                if foundShovel and foundRope then
                    continue = true
                    break
                end
            end
            if continue then
               -- do something i need to do
            end
        end
    end
-- Switches ID to corresponding index :
function GetToolIndex(id,retrn)
    local shovel = {
    [9598] = 4 , -- whacking driller of fate
    [9599]= 4 , -- whacking driller of fate(jammed)
    [9596]= 3 , -- squeezing gear of girlpower
    [9597]= 3 , -- squeezing gear of girlpower(jammed)
    [9594]= 2 , -- sneaky stabber of elitenesss
    [9595]= 2 , -- sneaky stabber of elitenesss(jammed)
    [5710]= 1, -- light shovel
    [3457] = 0 -- shovel
    }
    local rope = {
    [646]= 1, -- elvenhair rope
    [3003] = 0, -- rope
    [9598] = 4 , -- whacking driller of fate
    [9599]= 4 , -- whacking driller of fate(jammed)
    [9596]= 3 , -- squeezing gear of girlpower
    [9597]= 3 , -- squeezing gear of girlpower(jammed)
    [9594]= 2 , -- sneaky stabber of elitenesss
    [9595]= 2  -- sneaky stabber of elitenesss(jammed)
    }
    if retrn == 0 then 
        return shovel[id] 
    elseif return == 1 then 
        retrn rope[id] 
    end
end

但它不起作用,我想一定有更好的方法来实现这种方法,如果我需要在表中查找 X 值而不仅仅是 2 该怎么办?我希望我的问题能在这里得到理解。


嗯,将所有数据存储在表中比调用函数来获取容器数据要好得多。

无论如何,这是一个脚本,它将获取所有背包 id,将它们存储在表中,然后使用每个 id 调用您的函数。

Backpack = {data = {}};
function Backpack:update(refresh)
    if(#self.data==0 or refresh) then
        for i = 0, Container.GetLast():Index() do
            local cont = Container(i);
            for j = 0,cont:ItemCount()-1 do
                --table.insert(self.data,cont:GetItemData(j).id)
                self.data[#self.data+1] = cont:GetItemData(j).id; -- faster than table.insert
            end
        end
    end
end
function Backpack:refresh()
    self:update(true)
end
function Backpack:find(func,...) -- func should return a value if id is good, otherwise false, add extra args to call with the function
    if (not func) then
        return false;
    end
    self:update(); -- Incase there is no inventory data;
    for key,value in pairs(self.data) do
        local value = func(value,...); -- calls the function with the id (as first parameter) of the 'cached' user inventory
        if (value) then
            return value
        end
    end
    return false;
end

Example:

function GetToolIndex(id,return)
    local shovel = {
    [9598] = 4 , -- whacking driller of fate
    [9599]= 4 , -- whacking driller of fate(jammed)
    [9596]= 3 , -- squeezing gear of girlpower
    [9597]= 3 , -- squeezing gear of girlpower(jammed)
    [9594]= 2 , -- sneaky stabber of elitenesss
    [9595]= 2 , -- sneaky stabber of elitenesss(jammed)
    [5710]= 1, -- light shovel
    [3457] = 0 -- shovel
    }
    local rope = {
    [646]= 1, -- elvenhair rope
    [3003] = 0, -- rope
    [9598] = 4 , -- whacking driller of fate
    [9599]= 4 , -- whacking driller of fate(jammed)
    [9596]= 3 , -- squeezing gear of girlpower
    [9597]= 3 , -- squeezing gear of girlpower(jammed)
    [9594]= 2 , -- sneaky stabber of elitenesss
    [9595]= 2  -- sneaky stabber of elitenesss(jammed)
    }
    if return == 0 then 
        return shovel[id] 
    elseif return == 1 then 
        return rope[id] 
    end
end

function Test()
    local shovel,rope = Backpack:find(GetToolIndex,0),Backpack:find(GetToolIndex,1)
    if (shovel and rope) then
        print("Shovel and rope exist");
    end
end
Test();

EDIT:

经过思考一段时间后,您似乎尝试检查用户是否具有该特定 ID。

这是另一种方法,它将检查所有用户背包数据(深层表搜索),它将根据表键或表值进行搜索,它也适用于嵌套表,这应该对您有好处。

Backpack = {data = {}};
function Backpack:update(refresh) 
    if(#self.data==0 or refresh) then
        for i = 0, Container.GetLast():Index() do
            local cont = Container(i);
            for j = 0,cont:ItemCount()-1 do
                local data = cont:GetItemData(j); -- pretty sure this returns a table
                self.data[data.id] = data; -- self.data[9598] = {...}
            end
        end
    end
end
function Backpack:refresh()
    self:update(true)
end
function Backpack:MultiTableSearch(input,value,case,index_check)
    if (input and type(input) == 'table') then
        if (type(value) == 'table' and value == input) then
            return true;
        end
        for key,object in pairs(input) do
            if (index_check) then
                if (case and type(input)=='string' and type(key)=='string') then
                    if (value:lower() == key:lower()) then -- to avoid exit the loop
                        return true;
                    end
                else
                    if (key == value) then
                        return true
                    elseif(type(object)=='table') then
                        return self:MultiTableSearch(object,value,case,index_check)
                    end
                end
            else
                if (case and type(value)=='string' and type(object) == 'string') then
                    if (value:lower() == object:lower()) then
                        return true;
                    end
                elseif(type(object)=='table') then
                    if (value == object) then
                        return true;
                    else
                        return self:MultiTableSearch(object,value,case)
                    end
                else
                    if (object == value) then
                        return true;
                    end
                end
            end
        end
    end
    return false;
end
function Backpack:exists(value,case,index_check)
    self:update();
    return self:MultiTableSearch(self.data,value,case,index_check)
end
 -- checks the value 9598, case-insensitive is set to false,
 -- index_checking is set to true (checks table index --> Backpack.data[9598], if it was set it'll return true);
if (Backpack:exists(9598,false,true)) then
    print("User has whacking driller of fate");
end
if (Backpack:exists("Shovel of doom")) then -- will try to find any table-value that has the of "Shovel of doom" (case-sensitive)
    print("User Shovel of doom");
end

如果您担心 MultiTableSearch 的性能(因为它看起来有点重),它相当快,运行了几次测试。

Function

function MultiTableSearch(input,value,case,index_check)
    if (input and type(input) == 'table') then
        if (type(value) == 'table' and value == input) then
            return true;
        end
        for key,object in pairs(input) do
            if (index_check) then
                if (case and type(input)=='string' and type(key)=='string') then
                    if (value:lower() == key:lower()) then -- to avoid exit the loop
                        return true;
                    end
                else
                    if (key == value) then
                        return true
                    elseif(type(object)=='table') then
                        return MultiTableSearch(object,value,case,index_check)
                    end
                end
            else
                if (case and type(value)=='string' and type(object) == 'string') then
                    if (value:lower() == object:lower()) then
                        return true;
                    end
                elseif(type(object)=='table') then
                    if (value == object) then
                        return true;
                    else
                        return MultiTableSearch(object,value,case)
                    end
                else
                    if (object == value) then
                        return true;
                    end
                end
            end
        end
    end
    return false;
end

测试(将值附加到表中并扫描嵌套表,这两个测试都是无意义的表大小)

local start_time = os.clock();
t = {}
for i=1,500000 do --500k table size
    t[i]=i-1
end
print(os.clock()-start_time) -- 0.05 sec to create the table
local start_time = os.clock();
print(tostring(MultiTableSearch(t,500000,false)))-- will try to find a key with the value of 500,000
print(os.clock()-start_time) -- 0.197sec sec to scan the whole table

function nestedTable(object,times) -- creates nested table -> object={{{{..n times}}}}
    if (times > 0 ) then
        object[#object+1] = {times = times}
        return (nestedTable(object[#object],times-1))
    end
end
local start_time = os.clock();
t = {};
nestedTable(t,15000) --> will create table inside a table x 15000 times.
print(os.clock()-start_time) -- 0.007 sec to create the nested table
local start_time = os.clock();
print(tostring(MultiTableSearch(t,1,false)))-- will try to find a 1 (as a table value), the very last table value
print(os.clock()-start_time) -- 0.014 sec to find the value in the nested table
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

循环直到在表中找到 2 个特定值? 的相关文章

  • Kong - 验证上游 ssl(ssl_proxy 打开)

    我已经成功为 API 安装了 kong 网关 该 API 通过上游负载平衡到多个目标 应用程序服务器 现在 我有一个我的应用程序服务器的自签名证书 kong 和目标之间的 ssl 握手应该失败 我推断 kong 不验证上游证书 经过一些研究
  • 复杂的 SOLR 查询,包括 NOT 和 OR

    我对 SOLR 搜索有一些相当复杂的要求 我需要针对标记内容的数据库执行这些搜索 我需要首先过滤数据库以获取与我的过滤器标签匹配的结果 任何具有黑名单中的标签的结果都应被删除 除非它们也包含白名单中的标签 假设我想检索所有标记为 森林 或
  • 将搜索图标添加到输入框

    div div
  • .Net 中的计时器和循环准确吗?

    在开发一个程序时 计算555定时器IC产生的脉冲的频率和脉冲宽度 通过PC并行端口传到PC 我注意到每次运行代码时它都会显示不同的值 因此我开始测试循环和计时器的准确性 我运行了以下代码 发现它们不准确 我可能是错的 如果我错了 请纠正我
  • 如何编写循环来运行数据框的 t 检验?

    我遇到了对数据框中存储的某些数据运行 t 检验的问题 我知道如何一一做 但效率很低 请问如何写一个循环来实现呢 例如 我在testData中获取了数据 testData lt dput testData structure list Lab
  • GridSearchCV:每次函数完成循环时打印一些表达式

    假设你有一些功能function在Python中通过循环工作 例如 它可以是一个计算某个数学表达式的函数 例如x 2 对于数组中的所有元素 例如 1 2 100 显然这是一个玩具示例 是否可以编写这样的代码 每次function经过一个循环
  • 通过电子邮件搜索将 Excel 2003 中的数据行复制并粘贴到不同的工作表

    在任何人发表任何言论之前 我已经浏览了几篇与此类似想法相关的帖子 采用不同的搜索条件 然后对其进行修改 但我无法让宏正常工作 这可能是由于我缺乏编程知识 我想做的就是 search的电子邮件地址工作表1如果找到 则将整行复制到下一个空闲行工
  • Java for every循环工作

    我正在执行某项任务 当我无意中做错事时但代码执行并提供了正确的结果 我对此并不感到惊讶 并且想到每个循环的所有这些是如何工作的 示例 示例程序 public static void main String args String myInp
  • 为什么 LuaJIT 这么好?

    编辑 不幸的是 LuaJIT 已从下面链接的比较中删除 This 比较 http shootout alioth debian org u64 which programming languages are fastest php编程语言的
  • Python lambda 函数没有在 for 循环中正确调用[重复]

    这个问题在这里已经有答案了 我正在尝试使用 Python 中的 Tkinter 制作一个计算器 我使用 for 循环来绘制按钮 并且尝试使用 lambda 函数 以便仅在按下按钮时调用按钮的操作 而不是在程序启动时立即调用 然而 当我尝试这
  • 在lua中组合两个函数

    我刚开始学习lua 所以我的要求可能是不可能的 现在 我有一个接受函数的方法 function adjust focused window fn local win window focusedwindow local winframe w
  • xsl:for-each 循环内的计数器

    如何在 xsl for each 循环内获取一个计数器 该计数器将反映当前处理的元素的数量 例如我的源 XML 是
  • for 循环语法,其中包含 if 语句

    使用 if 语句编写 for 循环的普遍共识是什么 for int i 0 i lt hi i if some invalid condition continue if another invalid condition continue
  • 在数据框中搜索唯一值并用它们创建表

    自从我不久前开始使用 R URL 将类似于此示例格式 可在 源 列中找到 URL 中对我来说很重要的部分是 utm source ADX 位 我的数据如下所示 用户 来源 1 2 3 我需要做的是从 URL 中捕获 utm source 并
  • 为什么我的程序循环太多次?

    我是 C 初学者 正在尝试创建一个程序 但我的主要功能有问题 Problem 在询问他们想要输入多少个整数 例如 4 个数字 后 循环进行 5 次 基本上输入 5 个数字 它还只在第二个数字之后打印 Next In my while循环 我
  • 当 tableView 向下滑动时显示 UISearchController

    我通过 UISearchController 在我的测试应用程序中实现了搜索栏 当我启动应用程序时 我会在导航控制器下方看到搜索栏 但如何在应用程序启动时隐藏它并仅在下拉表格视图时显示它 并在拉出表格视图时再次隐藏 我在google或you
  • 带有 For 循环的多维数组 VBA

    尝试检查第一列中的值 即多维数组中的列 如果它匹配 则对另一列中与该行匹配的值进行排序 我认为我做错了 但这是我第一次搞乱多维数组 我是否需要在每个 for 循环中使用 UBound 和 LBound 来告诉它要查看哪一列 除了当前问题的答
  • 在多个 for 循环中使用相同的变量名是不好的做法吗?

    我只是使用 JSHint 来检查一些 JavaScript 代码 在代码中 我有两个 for 循环 它们的用法如下 for var i 0 i lt somevalue i 因此两个 for 循环都使用 var i 进行迭代 现在 JSHi
  • 如何匹配元音?

    我在处理我正在开发的一个更大程序的一个小组件时遇到了麻烦 基本上我需要让用户输入一个单词 并且需要打印第一个元音的索引 word raw input Enter word vowel aeiouAEIOU for index in word
  • 过早退出 Qualtrics 中的循环和合并块

    我目前正在进行一项 Qualtrics 调查 受访者必须解决一长串字谜问题 然后回答一些人口统计问题 为了使变位词部分更容易 我使用了循环和合并块 第一个字段是要解决的变位词 第二个字段是变位词的解决方案 因此调查可以根据受访者的答案来检查

随机推荐