generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Standard Way To Reverse Tables

Previous Thread :: Next Thread 
nickmaster24 is not online. nickmaster24
Joined: 04 Oct 2008
Total Posts: 8906
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 is not online. Quenty
Joined: 03 Sep 2009
Total Posts: 9316
05 Jun 2012 10:30 PM
Is it an array or a table?
Report Abuse
nickmaster24 is not online. nickmaster24
Joined: 04 Oct 2008
Total Posts: 8906
05 Jun 2012 10:31 PM
Aren't they the same thing? It is an array...

~Read Between The Squiggles~
Report Abuse
kingkiller1000 is not online. kingkiller1000
Joined: 12 Dec 2008
Total Posts: 26415
05 Jun 2012 10:36 PM
I think Quenty meant, is it 1D or 2D? Or 3D?
Report Abuse
nickmaster24 is not online. nickmaster24
Joined: 04 Oct 2008
Total Posts: 8906
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 is not online. 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 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
05 Jun 2012 11:07 PM
table.sort??
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
05 Jun 2012 11:09 PM
woops ignore my post ^.^
Report Abuse
ENET is not online. 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 is not online. 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 is not online. 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
Brandonhare is not online. Brandonhare
Joined: 02 May 2007
Total Posts: 11005
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 is not online. 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 is not online. 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 is not online. 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 is not online. 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 is not online. 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 is not online. 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 is not online. 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 is not online. Oysi
Joined: 06 Jul 2009
Total Posts: 9058
06 Jun 2012 12:20 PM
[ Content Deleted ]
Report Abuse
Occulisk is not online. 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 is not online. 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 is not online. Occulisk
Joined: 08 May 2012
Total Posts: 8
06 Jun 2012 12:39 PM
Thanks for giving me the straight answer.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image