home ~ projects ~ socials

Reverse A List In Lua

local function reverse_list(source)
  local response = {}
  local counter = #source
  while counter > 0 do
    table.insert(response, source[counter])
    counter = counter - 1
  end
  return response
end

local data = { "alfa", "echo", "delta", "bravo" }
local reversed = reverse_list(data)

for _, value in ipairs(reversed) do
  print(value)
end
Output:
bravo
delta
echo
alfa

Notes

  • I haven't done much lua. Wouldn't be surprised to find a more built-in way to do this, but this gets me what I'm looking for
-- end of line --