Lua数组包含用于进一步检查的特定值



很抱歉现在打扰你了,我还在学习。但我需要帮助。你能纠正我和脚本如何检查,然后得到数组2d的值进一步检查和计数点?

我构建的array2d语法示例:
role = {{[name],[points],[indexpoint]},
        {[...],[...],[...]}}

示例array2d值:

role = {{"mike", 30, "1"},
        {"michael", 40, "2"},
        {"mike", 40, "2"},
        {"michael", 50, "3"},
        {"frost", 50, "3"},
        {"nick", 60, "4"}}

我想要的是。当我搜索名字"michael"时。它将检测数组中的值。像这样

local player_data = {{"michael", 40, "2"},{"michael", 50, "3"}}

之后,我可以计算他已经得到的分数。40 + 50结果"90"将发送给新的变量,如resultpoint = 90

那么打印结果将显示如下

Player "Michael"
Your points is "90"
Here is the list of your index that you earned :
1. earn 40 points in index point "2"
2. earn 50 points in index point "3"

我的长代码:

role = {{"mike", "30", "1"},
        {"michael", "40", "2"},
        {"mike", "40", "2"},
        {"michael", "50", "3"},
        {"frost", "50", "3"},
        {"nick", "60", "4"}}
function check_role1(tab, val)
        for index, value in ipairs (tab) do
            -- We grab the first index of our sub-table instead for player name
            if value[1] == val then
                return true
            end
        end
        return false
    end
    function check_role2(tab, val)
        for index, value in ipairs (tab) do
            -- We grab the third index of our sub-table instead for index point
            if value[3] == val then
                return true
            end
        end
        return false
    end
    function detectroles(name)
        pn = name
        if check_role1 (role, pn) then
            print ('Yep')
            --[[for i = 1, #role do
                 player_checkname[i] = role[i][1] -- Get Player Name From Array for checking further
                 player_checkpnt[i] = role[i][2] -- Get Player Point From Array for checking further
                 player_checkidpnt[i] = role[i][3] -- Get Player Point From Array for checking further]]
             -- is this correct code to get value ?
            end
        else
            print ('You dont earn any points')
        end
    end
    detectroles("jack") -- this is call function, for checking name jack if he is in array or not

这真的可能吗?如果有一个简单的方法或更多更少的代码,让我知道。我知道,代码太多了。我还是新手

您似乎正在寻找的是一些称为filter(有时称为select)和reduce的一般数据结构函数。


filter是一个简单的函数,它对一组值进行操作,创建一个只包含符合给定谓词的值的新集合。filter的实现非常简单:

  • 创建一个新的空集
  • 遍历现有的集合,读取每个值
  • 将所有通过测试的值Push到新的set

操作的结果是新集合。

在Lua:

local function filter (list, test)
    local result = {}
    for index, value in ipairs(list) do
        if test(value, index) then
            result[#result + 1] = value
        end
    end
    return result
end

我们可以使用这个函数来获得一组经过过滤的值,其中每个表中的第一个条目是'michael':

local set = {
    { "mike", "30", "1" },
    { "michael", "40", "2" },
    { "mike", "40", "2" },
    { "michael", "50", "3" },
    { "frost", "50", "3" },
    { "nick", "60", "4" }
}
local filtered_set = filter(set, function (person)
    return person[1] == 'michael'
end)
for _, person in ipairs(filtered_set) do
    print(unpack(person))
end
--[[stdout:
  michael   40  2   
  michael   50  3
]]

reduce是一个通过迭代一组值来累积单个值的函数。reduce通常允许提供一个初始值,否则初始值是集合中的第一个值。

在Lua:

local function reduce (set, action, initial_value)
    local result
    local index
    if initial_value ~= nil then
        result = initial_value
        index = 1
    else
        result = set[1]
        index = 2
    end
    for i = index, #set do
        result = action(result, set[i], i)
    end
    return result
end

可用于确定集合项的组合值:

local value = reduce(filtered_set, function (score, next_entry)
    return score + next_entry[2] -- Careful with relying on stringly-math
end, 0)
print(value) --> prints 90

虽然在Lua标准库中没有,但这些是非常常见的函数集合操作,学习如何实现它们(以及其他像each, map, reject, count, index, has, find)将教会你很多关于使用数据结构的知识。

相关内容

  • 没有找到相关文章

最新更新