How would you swap a value in a table and exchange it for a different one? for example:
local TestTable = {1, 2, 3, 4, 5}
local SwappedTestTable = TestTable:Swap(2, 4)
for i, v in pairs(TestTable) do
print(v)
end
-- Output = 1, 4, 3, 2, 5
This is what I've tried and it failed horrendously.
local TestTable = {1, 2, 3, 4, 5}
function Swap(Table, Pos1, Pos2)
local Table = Table
local Item1 = Table[Pos1]
local Item2 = Table[Pos2]
table.remove(Table, Pos1)
table.insert(Table, Pos2, Item1)
table.remove(Table, Pos2)
table.insert(Table, Pos1, Item2)
return Table
end
for _, v in pairs(Swap(TestTable, 2, 4)) do
print(v)
end