Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果我有一个列表(表格):
local list = {'foo', 'bar', 'baz', 'qux'}
如何从最后获得第n个项目?(例如,最后一个或倒数第二个)
尝试从末尾list[#list+1-n]获取第n个条目,在 Lua 中像往常一样从 1 开始计数。所以最后一项有n = 1。
list[#list+1-n]
这应该工作
function getEntryFromEnd(table, entry) local count = (table and #table or false) if (count) then return table[count-entry]; end return false; end