我构建的示例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"}}
我想要的是什么。当我在寻找名字“迈克尔”。它会检测数组中的值。像这样的东西
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
这真的有可能吗?如果有简单的方法或更少的代码,请告诉我。我知道,代码太多了。我还是新手
答案 0 :(得分: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
,{{ 1}},reject
,count
,index
,has
)会教您如何使用数据结构。
尝试考虑它们如何适合您当前的代码。