|
| 30 Oct 2017 04:48 PM |
Can someone explain me some more regarding Tables? Tia. |
|
|
| Report Abuse |
|
|
doggy00
|
  |
| Joined: 11 Jan 2011 |
| Total Posts: 3571 |
|
|
| 30 Oct 2017 04:59 PM |
So you want to reverse all the values of a table?
tbl = {table values here}
for i,v in pairs(tbl) do
table.insert(tbl,v)
end
|
|
|
| Report Abuse |
|
|
doggy00
|
  |
| Joined: 11 Jan 2011 |
| Total Posts: 3571 |
|
|
| 30 Oct 2017 05:00 PM |
| sorry actually that wouldn't work let me write a code that would, i wasn't thinking clearly lol |
|
|
| Report Abuse |
|
|
|
| 30 Oct 2017 05:03 PM |
i don't usually flip tables (i eat on them most of the times) but i'd use hands if i had to
|
|
|
| Report Abuse |
|
|
|
| 30 Oct 2017 05:08 PM |
local function reverse(arr) local i,j=1,#arr while j>i do arr[i],arr[j]=arr[j],arr[i] i=i+1 j=j-1 end end
--example usage local t={5,15,1,6,7,2,4} reverse(t)
for i=1,#t do print(t[i]) end
|
|
|
| Report Abuse |
|
|
doggy00
|
  |
| Joined: 11 Jan 2011 |
| Total Posts: 3571 |
|
|
| 30 Oct 2017 05:09 PM |
| this is probably more complicated than it needs to be but it still works tbl = ################### lastval = tbl[#tbl] for i = ###### do table.insert(tbl,tbl[#tbl-(i-1)]) table.remove(tbl,tbl[#tbl-1]) end table.remove(tbl,1) table.insert(tbl,1,lastval) for i,v in pairs(tbl) do print(v) end |
|
|
| Report Abuse |
|
|
doggy00
|
  |
| Joined: 11 Jan 2011 |
| Total Posts: 3571 |
|
|
| 30 Oct 2017 05:10 PM |
oops forgot to preview
paystebin D4zXwc7J |
|
|
| Report Abuse |
|
|
|
| 30 Oct 2017 05:13 PM |
function reverse(tbl) local newtbl={} for i=#tbl,1,-1 do newtbl[#newtbl+1]=tbl[i] end return newtbl end |
|
|
| Report Abuse |
|
|
|
| 30 Oct 2017 05:13 PM |
There are two types of tables: Arrays and Dictionaries
Array = {"Value1", "Value2", etc...} Dictionary = {First = "Value1", Type2 = "Value2", etc...}
Getting the values out of these tables is called indexing. In order to index an array you type in Array[1] or Array[3]. You can also set the value of an array by typing Array[4] = "Hello".
In order to index a dictionary there are two options. Dictionary["First"] or Dictionary.First either works and setting values is the same as arrays.
The main point here is that only arrays can be reversed because they have a sense of "orientation". 3 comes after 2 which comes after 1. In a dictionary there's not really that sense since the keys are strings and not numbers.
In order to reverse an array then, you can do this:
function reverse(array) local reversedArray = {} for i=#array, 1 do table.insert(reversedArray, array[i]) end return reversedArray end
|
|
|
| Report Abuse |
|
|
doggy00
|
  |
| Joined: 11 Jan 2011 |
| Total Posts: 3571 |
|
|
| 30 Oct 2017 05:13 PM |
okay nevermind that one isn't right either lol
07mEc1pm |
|
|
| Report Abuse |
|
|
|
| 30 Oct 2017 05:15 PM |
whats wrong with mine ):
mine does it in-place so it doesn't even create a new array. probably the most efficient way of doing it as it uses no functions or anything and is linear as it accesses each element only once.
|
|
|
| Report Abuse |
|
|
|
| 30 Oct 2017 05:35 PM |
^ No one said there's anything wrong with yours, both of ours accomplish the same task in a different way. In terms of efficiency it's actually the same, I ran some tests and the time taken to run each 1k times is nearly the same.
The beauty of programming is how the same task can be accomplished in so many ways.
|
|
|
| Report Abuse |
|
|
doggy00
|
  |
| Joined: 11 Jan 2011 |
| Total Posts: 3571 |
|
|
| 30 Oct 2017 06:02 PM |
| ^ I think mine should always work too but it's definitely less efficient as far as how the code is written. I'm honestly such a rookie that I didn't even know you could use loops to go backwards until seeing this thread's replies, lol. |
|
|
| Report Abuse |
|
|
Quasiduck
|
  |
| Joined: 28 Sep 2008 |
| Total Posts: 2437 |
|
| |
|