What is a good way to reverse a table I want to make something like this .
table={1, 2, 3, 4, 5, 6, 7,8 ,9, 10}
to
table={10, 9, 8, 7, 6, 5, 4, 3, 2,1}
thanks for help!
What is a good way to reverse a table I want to make something like this .
table={1, 2, 3, 4, 5, 6, 7,8 ,9, 10}
to
table={10, 9, 8, 7, 6, 5, 4, 3, 2,1}
thanks for help!
This code copies the content into a new table, reversed.
local lengthOfTable = #myTable for i = 0, lengthOfTable-1 do newTable[#newTable+1] = myTable[lengthOfTable-i] end
On a side note: depending on the type of content, you can use the sort command as well.
First, you should never name a table “table”. There is an object of table manipulation function called “table”.
As for the code:
local newTable = {} for i = #oldTable, 1, -1 do newTable[#newTable + 1] = oldTable[i] end
Or something like that.
I just edited my code because it was missing a “+1” somewhere. Better now.
Rob, I think your code doesn’t reverse the order.
You’re right, I updated the code to do reverse order.
Rob
This code copies the content into a new table, reversed.
local lengthOfTable = #myTable for i = 0, lengthOfTable-1 do newTable[#newTable+1] = myTable[lengthOfTable-i] end
On a side note: depending on the type of content, you can use the sort command as well.
First, you should never name a table “table”. There is an object of table manipulation function called “table”.
As for the code:
local newTable = {} for i = #oldTable, 1, -1 do newTable[#newTable + 1] = oldTable[i] end
Or something like that.
I just edited my code because it was missing a “+1” somewhere. Better now.
Rob, I think your code doesn’t reverse the order.
You’re right, I updated the code to do reverse order.
Rob