Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get n-th element from end of list (table)

If I have a list (table):

local list = {'foo', 'bar', 'baz', 'qux'}

How do I get the n-th item from the end? (e.g., the last or second to last)

like image 429
Uyghur Lives Matter Avatar asked Mar 17 '23 16:03

Uyghur Lives Matter


1 Answers

Try list[#list+1-n] to get the n-th entry from the end, counting from 1 as usual in Lua. So the last item has n=1.

like image 177
lhf Avatar answered Mar 30 '23 12:03

lhf