Table.flip: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
Line 11: Line 11:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function table.flip(theTable)
function table.flip(theTable)
if (type(theTable) == "table") then
    if (type(theTable) == "table") then
local newTable = {}
        local newTable = {}
local tableNumber = -1
        local tableNumber = -1
for i = 1, #theTable do
        for i = 1, #theTable do
tableNumber = tableNumber + 1
            tableNumber = tableNumber + 1
table.insert(newTable, theTable[#theTable-tableNumber])
            table.insert(newTable, theTable[#theTable-tableNumber])
if (i == #theTable) then
            if (i == #theTable) then
return newTable
                return newTable
end
            end
end
        end
end
    end
return false
    return false
end
end
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
==Example==
==Example==
<section name="serverside or clientside Script" class="both" show="true">
<section name="serverside or clientside Script" class="both" show="true">

Revision as of 07:22, 1 October 2023

Syntax

string table.flip( theTable )

Required Arguments

  • theTable: The table to flip.

Returns

Returns the table with flip values

Click to collapse [-]
Shared function
function table.flip(theTable)
    if (type(theTable) == "table") then
        local newTable = {}
        local tableNumber = -1
        for i = 1, #theTable do
            tableNumber = tableNumber + 1
            table.insert(newTable, theTable[#theTable-tableNumber])
            if (i == #theTable) then
                return newTable
            end
        end
    end
    return false
end

Example

Click to collapse [-]
serverside or clientside Script
local myTable = {"test_1", "test_2", "test_3", "test_4", "test_5"}
local flipedTable = table.flip(myTable)
for k, v in ipairs(flipedTable) do
    outputChatBox(v)
end

--// Author: iManGaaX - Youssef Maged (discord: imangaax)