Jump to content

reverse elements in table


iPanda

Recommended Posts

Hi guys. How can i do reverse all elements in table?

Example:

  
--source 
local test = { 
  {'13'}, 
  {'42'}, 
  {'51'}, 
} 
--reverse 
local test = { 
  {'51'}, 
  {'42'}, 
  {'13'}, 
} 
  

The problem was solved by itself. Can anyone come in handy:

function table.reverse(t) 
    local reversedTable = {} 
    local itemCount = #t  
    for k, v in ipairs(t) do 
        reversedTable[itemCount + 1 - k] = v  
    end 
    return reversedTable  
end 

Link to comment
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...