lua 数组包含用于进一步检查的特定值
lua array contains specific value for checking further
对不起,如果我现在打扰你了,我还在学习。但我需要帮助。你能纠正我并编写脚本如何检查然后获取数组 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
的实现非常简单:
- 创建一个新的空集
- 遍历现有集合,读取每个值
- 将通过测试的任何值推入新集合
运算结果为新集合。
在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
我们可以使用这个函数来获取一组过滤值,其中每个 table 中的第一个条目是 '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
) 会教你很多关于数据结构的知识。
尝试考虑它们如何适合您当前的代码。
对不起,如果我现在打扰你了,我还在学习。但我需要帮助。你能纠正我并编写脚本如何检查然后获取数组 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
的实现非常简单:
- 创建一个新的空集
- 遍历现有集合,读取每个值
- 将通过测试的任何值推入新集合
运算结果为新集合。
在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
我们可以使用这个函数来获取一组过滤值,其中每个 table 中的第一个条目是 '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
) 会教你很多关于数据结构的知识。
尝试考虑它们如何适合您当前的代码。