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: Flip the table.

Previous Thread :: Next Thread 
milanomaster is not online. milanomaster
Joined: 22 Jan 2013
Total Posts: 252
30 Oct 2017 04:48 PM
Can someone explain me some more regarding Tables?
Tia.
Report Abuse
doggy00 is online. 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 is online. 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
Valyrian_Advisor is not online. Valyrian_Advisor
Joined: 13 Apr 2013
Total Posts: 1703
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
newdumbman2 is online. newdumbman2
Joined: 01 Apr 2010
Total Posts: 3265
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 is online. 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 is online. doggy00
Joined: 11 Jan 2011
Total Posts: 3571
30 Oct 2017 05:10 PM
oops forgot to preview

paystebin D4zXwc7J
Report Abuse
chexyalani is online. chexyalani
Joined: 26 Mar 2017
Total Posts: 694
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
ExtremeBuilder15 is online. ExtremeBuilder15
Joined: 01 May 2012
Total Posts: 3176
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 is online. 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
newdumbman2 is online. newdumbman2
Joined: 01 Apr 2010
Total Posts: 3265
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
ExtremeBuilder15 is online. ExtremeBuilder15
Joined: 01 May 2012
Total Posts: 3176
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 is online. 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 is not online. Quasiduck
Joined: 28 Sep 2008
Total Posts: 2437
30 Oct 2017 06:33 PM
(╯°□°)╯︵ ┻━┻
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