如何在LuaScript中获取和打印表查询值的最后索引


JobList_Table = {}
function CheckList() --> Called whenever someone makes a purchase.
table.insert(JobList_Table,1,
{
["BuyerName"] = "Marie", -->Simulating with the name Marie - In the original script this name is added by a function.
["LatestPurchases"] = "Potatoes", -->Simulating with Potato 
["DateTime"] = os.time()
}
)
--[[ ->for simulate in print,keep the JobList_Table,1,because that's how the program inserts
table.insert(JobList_Table,1,
{
["BuyerName"] = "Marie",
["LatestPurchases"] = "Strawberries",
["DateTime"] = os.time()
}
)     
table.insert(JobList_Table,1,
{
["BuyerName"] = "Emma", 
["LatestPurchases"] = "Chayote",
["DateTime"] = os.time()
}     
)--]]
end
function ShowList() -->Executed for verification.
local NewUser = 1
local ExecutionName = "Marie"
if NewUser ~= 0 then
for Index,Data in ipairs(JobList_Table) do
if Data.BuyerName ~= nil and Data.BuyerName == ExecutionName then  
print(Data.BuyerName, Data.LatestPurchases, Data.DateTime) --LOGS SHOW
return
end
end

print("Dont Find Or Nil")
return
end
print("Is Not New User")
end

这个脚本的作用:

每当有人购买时,脚本都会添加";BuyerName"LatestPurchases"DateTime"。您可以通过运行ShowList((函数来查看最后一次购买


我需要什么:目前我只能加载每个BuyerName的最后一次购买的日志,我想最多能看到5次最后一次购物的日志,也就是说,如果这个人买了1次,就会出现1个日志,如果他买了10次,最后5次购物就会出现

例如,在SQLServer/Query中,我会这样做:从JobList_Table中选择前5名BuyerName,LatestPurchases,DateTime,其中BuyerName=";Marie";ORDER BY IndexID DESC

我知道";ShowList(("函数可能需要重做,但我不知道怎么做,我一直在研究lua表,但我真的被这个卡住了。有人能帮我举几个例子吗?感谢

由于新的购买总是添加到表的顶部,因此您可以通过y轻松获得最新的x购买量,如下所示:

purchases = {}
-- 9 purchases, 6 by "Marie"
table.insert(purchases, 1, {BuyerName = "Marie", LatestPurchases = "Potatoes", DateTime = os.time()})
table.insert(purchases, 1, {BuyerName = "Marie", LatestPurchases = "Copper Coin", DateTime = os.time()})
table.insert(purchases, 1, {BuyerName = "Henry", LatestPurchases = "Flowers", DateTime = os.time()})
table.insert(purchases, 1, {BuyerName = "Marie", LatestPurchases = "Galvanized Nails", DateTime = os.time()})
table.insert(purchases, 1, {BuyerName = "Marie", LatestPurchases = "Alligator Clips", DateTime = os.time()})
table.insert(purchases, 1, {BuyerName = "Arnold", LatestPurchases = "Birthday Card", DateTime = os.time()})
table.insert(purchases, 1, {BuyerName = "Marie", LatestPurchases = "Voltmeter", DateTime = os.time()})
table.insert(purchases, 1, {BuyerName = "Winston", LatestPurchases = "Bananas", DateTime = os.time()})
table.insert(purchases, 1, {BuyerName = "Marie", LatestPurchases = "Bottled Water", DateTime = os.time()})
-- Function to get up to x latest purchases by name
function getLatestPurchasesByName(name, limit)
local latestItemCount = 0
local itemList = {}
for _, v in pairs(purchases) do
if v.BuyerName == name then
table.insert(itemList, v)
latestItemCount = latestItemCount + 1
if latestItemCount >= limit then
return itemList
end
end
end
return itemList
end
-- Requesting latest 5 purchases from Marie, then printing the table
result = getLatestPurchasesByName("Marie", 5)
for _, v in pairs(result) do
print(v.BuyerName, v.LatestPurchases)
end

相关内容

  • 没有找到相关文章

最新更新