|
| 05 Jun 2012 10:29 PM |
I need to completely reverse a table; is there a method to do so?
~Read Between The Squiggles~ |
|
|
| Report Abuse |
|
|
Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 05 Jun 2012 10:30 PM |
| Is it an array or a table? |
|
|
| Report Abuse |
|
|
|
| 05 Jun 2012 10:31 PM |
Aren't they the same thing? It is an array...
~Read Between The Squiggles~ |
|
|
| Report Abuse |
|
|
|
| 05 Jun 2012 10:36 PM |
| I think Quenty meant, is it 1D or 2D? Or 3D? |
|
|
| Report Abuse |
|
|
|
| 05 Jun 2012 10:37 PM |
I think this'll work:
for i = 1, #FinalPath do Flipped[#FinalPath-i+1] = FinalPath[i] end
It's an 1 by x matrix :P
~Read Between The Squiggles~ |
|
|
| Report Abuse |
|
|
ENET
|
  |
| Joined: 01 Jan 2010 |
| Total Posts: 4820 |
|
|
| 05 Jun 2012 11:00 PM |
Function doesn't create a copy of a table thus uses less memory.
function revTble(tble) local value1, value2; local pos1, pos2; pos1 = #tble; pos2 = 1; for i=math.floor(#tble/2),#tble do print('Positions: ('..pos1..','..pos2..')') value1 = tble[pos1]; value2 = tble[pos2]; tble[pos1] = value2; tble[pos2] = value1; pos1 = pos1-1; pos2 = pos2+1; if(pos1 == pos2)then return tble; end end end |
|
|
| Report Abuse |
|
|
smurf279
|
  |
| Joined: 15 Mar 2010 |
| Total Posts: 6871 |
|
| |
|
smurf279
|
  |
| Joined: 15 Mar 2010 |
| Total Posts: 6871 |
|
| |
|
ENET
|
  |
| Joined: 01 Jan 2010 |
| Total Posts: 4820 |
|
|
| 05 Jun 2012 11:19 PM |
Indented version
function revTble(tble) local value1, value2; local pos1, pos2; pos1 = #tble; pos2 = 1; for i=math.floor(#tble/2),#tble do value1 = tble[pos1]; value2 = tble[pos2]; tble[pos1] = value2; tble[pos2] = value1; pos1 = pos1-1; pos2 = pos2+1; if(pos1 == pos2)then return tble; end end end |
|
|
| Report Abuse |
|
|
belial52
|
  |
| Joined: 10 Oct 2009 |
| Total Posts: 8074 |
|
|
| 05 Jun 2012 11:23 PM |
@Enet, Why not just do
for i = 1, #YourTable, -1 do newTable = YourTable[i] end
? |
|
|
| Report Abuse |
|
|
ENET
|
  |
| Joined: 01 Jan 2010 |
| Total Posts: 4820 |
|
|
| 05 Jun 2012 11:36 PM |
| Cause it stores two new tables into the scope. Memory consumption. |
|
|
| Report Abuse |
|
|
|
| 06 Jun 2012 12:17 AM |
for i = 1, #tab/2 do tab[i],tab[#tab-i+1] = tab[#tab-i+1],tab[i] end |
|
|
| Report Abuse |
|
|
Occulisk
|
  |
| Joined: 08 May 2012 |
| Total Posts: 8 |
|
|
| 06 Jun 2012 01:10 AM |
Here's a way to do it without any iteration, this is probably the fastest way it can be done.
In my opinion, speed optimization is a lot more important than memory optimization in this case.
tmpTab,retTab = {},{} function reverseTable(tab) local i = 0.5 tmpTab,retTab = tab,{} assert(loadstring(string.gsub(string.sub(string.rep(";retTab[index]=tmpTab[#tmpTab-index+1]",#tab),2),"index",(function() i=i+0.5;return math.floor(i) end))))() return retTab end |
|
|
| Report Abuse |
|
|
agent767
|
  |
| Joined: 03 Nov 2008 |
| Total Posts: 4181 |
|
|
| 06 Jun 2012 01:59 AM |
local tablelen = #FinalPath for i = 1,math.ceil(#tablelen/2) do local val = FinalPath[i] FinalPath[i]=FinalPath[tablelen-i] FinalPath[tablelen-i]=val end
maybe? |
|
|
| Report Abuse |
|
|
NXTBoy
|
  |
| Joined: 25 Aug 2008 |
| Total Posts: 4533 |
|
|
| 06 Jun 2012 03:50 AM |
Brandonhare's answer is the best one.
@Occulisk: in what universe are string operations faster than a for loop. And really, loadstring? |
|
|
| Report Abuse |
|
|
Occulisk
|
  |
| Joined: 08 May 2012 |
| Total Posts: 8 |
|
|
| 06 Jun 2012 11:04 AM |
Sorry, I'm kind of new here, I guess I was wrong :/
What's wrong with loadstring? |
|
|
| Report Abuse |
|
|
agent767
|
  |
| Joined: 03 Nov 2008 |
| Total Posts: 4181 |
|
|
| 06 Jun 2012 11:10 AM |
It`s horribly inefficient and there are like 1000 better ways to do it. Seriously-don`t use loadstring. Even I would never use it for such things even though I don`t have an inefficiency-phobia like most people in this forum... |
|
|
| Report Abuse |
|
|
Occulisk
|
  |
| Joined: 08 May 2012 |
| Total Posts: 8 |
|
|
| 06 Jun 2012 12:11 PM |
| What makes it inefficient? What are the alternatives? |
|
|
| Report Abuse |
|
|
agent767
|
  |
| Joined: 03 Nov 2008 |
| Total Posts: 4181 |
|
|
| 06 Jun 2012 12:18 PM |
look at the other comments... when manipulating a table tablemanipulation is always better than stringmanipulation... |
|
|
| Report Abuse |
|
|
Oysi
|
  |
| Joined: 06 Jul 2009 |
| Total Posts: 9058 |
|
| |
|
Occulisk
|
  |
| Joined: 08 May 2012 |
| Total Posts: 8 |
|
|
| 06 Jun 2012 12:26 PM |
Ok, that still didn't answer my question.
Why shouldn't I use loadstring in general? Why is it inefficient? Isn't it the same as running lua code? |
|
|
| Report Abuse |
|
|
ENET
|
  |
| Joined: 01 Jan 2010 |
| Total Posts: 4820 |
|
|
| 06 Jun 2012 12:27 PM |
[[Why shouldn't I use loadstring in general? Why is it inefficient? Isn't it the same as running lua code?]]
No because it's additional code the luaVM has to parse. |
|
|
| Report Abuse |
|
|
Occulisk
|
  |
| Joined: 08 May 2012 |
| Total Posts: 8 |
|
|
| 06 Jun 2012 12:39 PM |
| Thanks for giving me the straight answer. |
|
|
| Report Abuse |
|
|