Created
July 16, 2012 10:59
-
-
Save balaam/3122129 to your computer and use it in GitHub Desktop.
Reverse an ipairs table in Lua (not clever O(n) could be O(n/2) with swapping)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- Reverses an ipairs table | |
-- | |
--local r = ReverseTable({'a', 'b', 'c'}) | |
--for k, v in ipairs(r) do | |
-- print(k, v) | |
--end | |
-- | |
-- | |
function ReverseTable(t) | |
local reversedTable = {} | |
local itemCount = #t | |
for k, v in ipairs(t) do | |
reversedTable[itemCount + 1 - k] = v | |
end | |
return reversedTable | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
or modified ipairs-function