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
 

pairs VS for

Previous Thread :: Next Thread 
rangersmash is not online. rangersmash
Joined: 18 Nov 2009
Total Posts: 2891
28 Mar 2014 05:48 PM
Hi,

Just out of intrest, which do you guys use and which is actually better to use?

for i,v in pairs(game.Players:GetChildren()) do

end

OR?

players = game.Players:GetChildren()
for i = 1, #players do

end

Is there any advantage over each other, either scripting or server side (lag?)

Thanks for any feedback :P
Report Abuse
VilleSlay is not online. VilleSlay
Joined: 14 May 2011
Total Posts: 1405
28 Mar 2014 05:52 PM
>"pairs VS for"

That makes absolutely no sense at all considering both of them are for-loops.

~~> Ville <~~
Report Abuse
rangersmash is not online. rangersmash
Joined: 18 Nov 2009
Total Posts: 2891
28 Mar 2014 05:53 PM
Well, sorry about that. What would you call them then?
Report Abuse
Locky2013 is not online. Locky2013
Joined: 14 Feb 2012
Total Posts: 2401
28 Mar 2014 05:54 PM
If you know what he means then WHY are you posting useless and senseless comments.
Report Abuse
lolb3 is not online. lolb3
Joined: 16 Jan 2010
Total Posts: 2268
28 Mar 2014 06:51 PM
they're called a generic for loop and numeric for loop, respectfully
Report Abuse
EchoReaper is not online. EchoReaper
Joined: 14 Oct 2008
Total Posts: 4323
28 Mar 2014 07:02 PM
I personally find pairs easier to use in most cases. v is much easier to type than players[i]. On top of that, if a value in the table is modified while you're searching through without using pairs, you will no longer be able to use it unless you specifically compensate for that in your code. Also, if you have values stored in your table such as "val = 5", you won't be able to get the name of the variable unless you're using pairs.

The only useful thing I can see using a plain for loop to run through tables with is if you want to start/stop(without using a break) at any given points in the table.
Report Abuse
EchoReaper is not online. EchoReaper
Joined: 14 Oct 2008
Total Posts: 4323
28 Mar 2014 07:03 PM
", you will no longer be able to use it"

Correction: You will no longer be able to use it as intended. You may still get values back, but they might not be what you expected them to be.
Report Abuse
lolb3 is not online. lolb3
Joined: 16 Jan 2010
Total Posts: 2268
28 Mar 2014 07:30 PM
also, adding to what Echo said, numeric for loops are good for tweening and fades.
Report Abuse
wazap is not online. wazap
Joined: 29 Jun 2007
Total Posts: 23234
28 Mar 2014 10:03 PM
for loops are faster but more error prone than the generic version. The generic version, however, are the only ones that can loop through tables with non-numeric indecies.
Report Abuse
MrChubbs is not online. MrChubbs
Joined: 14 Oct 2010
Total Posts: 4969
29 Mar 2014 12:54 AM
@Wazap, depends on implementation.
@lolb, I'd be remiss if I didn't point out that pairs is an enumeration, not a generic loop.
Report Abuse
k9rosie is not online. k9rosie
Joined: 05 Jul 2008
Total Posts: 14339
29 Mar 2014 04:12 AM
For loops are number counters.

pairs() et al. are used to iterate data over a table. You can use for loops to do the exact same thing pairs() does, but pairs() is simpler.

to answer your second question, I was actually curious myself so I tested this snippet of code I wrote because boredom:

--------------------------------
t = {}
time = 0
--completely irrelevant, used to insert a large amount of data into the table
for i = 0, 1*10^4 do
table.insert(t, i)
end

--for loop bench
time = os.clock() --our stopwatch
for i = 0, #t do
print(t[i])
end
result1 = os.clock() - time --save our time
for k,v in pairs(t) do
print(v)
end
result2 = os.clock() - time --save our second time



print(string.format("elapsed for loop time: %.2f seconds\n", result1))
print(string.format("elapsed pair() loop time: %.2f seconds\n", result2))
--------------------------------

I found that using pairs() actually takes twice the amount of time than for loops do to iterate through a table. i thought that was interesting

Report Abuse
OneTrueGodTheHolyCow is not online. OneTrueGodTheHolyCow
Joined: 24 Oct 2013
Total Posts: 9350
29 Mar 2014 05:06 AM
if yu do for i, v in pairs(blah) its slower than for i = 1, #blah

but dont worry about it unless your table is like 100000 long
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