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

门治

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

这是与游戏相关的代码的摘录,它在每个背包的每个插槽中循环穿过,直到找到铁锹和绳索为止

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

例子:

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();

编辑:

经过一会儿的思考,您似乎尝试检查用户是否具有该特定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 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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

vba循环直到找到特定值

来自分类Dev

搜索列中2个值之间的所有值并循环直到找到最后一个

来自分类Dev

如何循环播放,直到在Node.js中找到包含特定数据的文件?

来自分类Dev

如何循环播放,直到在Node.js中找到包含特定数据的文件?

来自分类Dev

在mysql的四个表中找到最大值

来自分类Dev

程序从两个哈希表中找到相交的值

来自分类Dev

如何从一个表中找到多个值?

来自分类Dev

在SQL中使用ORDER BY的RECORD COUNT,直到在列中找到特定值为止

来自分类Dev

从表中找到合适的值

来自分类Dev

如何在2个多维数组中找到公共值

来自分类Dev

单击循环直到在java中找到多个按钮的(按钮)元素

来自分类Dev

在循环中找到最大值

来自分类Dev

如何计算条件的TRUE值,直到在R中找到FALSE

来自分类Dev

在循环内进行异步调用,直到找到一个值

来自分类Dev

从2列中找到一个唯一值,然后找到这些值的比率

来自分类Dev

在字符串中找到特定字符后,如何创建一个插入字符的循环?

来自分类Dev

在另一个表中找到关联值时,Oracle在列中设置值

来自分类Dev

在sqlalchemy中找到表之间的缺失值

来自分类Dev

在R中的循环序列中找到3个连续的最大值

来自分类Dev

代码无法从JSON中找到特定值

来自分类Dev

从var值中找到特定的模式

来自分类Dev

宏/函数,在一列中找到一个字符串,然后在工作表2中的一个句子中找到它,并在第3列中返回各自的值

来自分类Dev

有什么方法可以在Microsoft SQL Server的每个表的每个字段中找到特定值?

来自分类Dev

如何通过该表中的另一个相关值在表中找到一个值?

来自分类Dev

当before值不为1时,则将before值代入当前值,直到在R中找到下一个非1值

来自分类Dev

在 hive 中找到第 n 个值

来自分类Dev

在哈希表中找到多少个具有相同值的键?

来自分类Dev

如何循环直到一个特定值或通过输入再次请求

Related 相关文章

  1. 1

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

  2. 2

    vba循环直到找到特定值

  3. 3

    搜索列中2个值之间的所有值并循环直到找到最后一个

  4. 4

    如何循环播放,直到在Node.js中找到包含特定数据的文件?

  5. 5

    如何循环播放,直到在Node.js中找到包含特定数据的文件?

  6. 6

    在mysql的四个表中找到最大值

  7. 7

    程序从两个哈希表中找到相交的值

  8. 8

    如何从一个表中找到多个值?

  9. 9

    在SQL中使用ORDER BY的RECORD COUNT,直到在列中找到特定值为止

  10. 10

    从表中找到合适的值

  11. 11

    如何在2个多维数组中找到公共值

  12. 12

    单击循环直到在java中找到多个按钮的(按钮)元素

  13. 13

    在循环中找到最大值

  14. 14

    如何计算条件的TRUE值,直到在R中找到FALSE

  15. 15

    在循环内进行异步调用,直到找到一个值

  16. 16

    从2列中找到一个唯一值,然后找到这些值的比率

  17. 17

    在字符串中找到特定字符后,如何创建一个插入字符的循环?

  18. 18

    在另一个表中找到关联值时,Oracle在列中设置值

  19. 19

    在sqlalchemy中找到表之间的缺失值

  20. 20

    在R中的循环序列中找到3个连续的最大值

  21. 21

    代码无法从JSON中找到特定值

  22. 22

    从var值中找到特定的模式

  23. 23

    宏/函数,在一列中找到一个字符串,然后在工作表2中的一个句子中找到它,并在第3列中返回各自的值

  24. 24

    有什么方法可以在Microsoft SQL Server的每个表的每个字段中找到特定值?

  25. 25

    如何通过该表中的另一个相关值在表中找到一个值?

  26. 26

    当before值不为1时,则将before值代入当前值,直到在R中找到下一个非1值

  27. 27

    在 hive 中找到第 n 个值

  28. 28

    在哈希表中找到多少个具有相同值的键?

  29. 29

    如何循环直到一个特定值或通过输入再次请求

热门标签

归档