1

对不起,如果我现在打扰你,我还在学习。但我需要帮助。你能纠正我并编写脚本如何检查然后获取数组 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

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

4

1 回答 1

1

您似乎正在寻找一些通用数据结构函数,称为filter(有时称为select)和reduce.


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

  • 创建一个新的空集
  • 遍历您现有的集合,读取每个值
  • 将任何通过测试的值推送到新集合中

操作的结果是新的集合。

在 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)将教你很多关于使用数据结构的知识。

尝试考虑它们如何适合您当前的代码。

于 2016-11-09T04:05:53.540 回答