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: Pairs vs Next

Previous Thread :: Next Thread 
TheeDeathCaster is not online. TheeDeathCaster
Joined: 19 Jun 2011
Total Posts: 1191
12 Jul 2016 02:58 PM
Which is better to use for iterating through a table?

local tbl = {
'Which ',
'is ',
'better',
'?'
}

for i, v in pairs(tbl) do
print(i, v)
end

for i, v in next, tbl do
print(i, v)
end


Now, look up in the upper-right corner of your screen, that red box there: that's the door.
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
12 Jul 2016 03:08 PM
It's the same thing.


Report Abuse
TheeDeathCaster is not online. TheeDeathCaster
Joined: 19 Jun 2011
Total Posts: 1191
12 Jul 2016 03:11 PM
@Jarod Doesn't really answer my question. lol


Now, look up in the upper-right corner of your screen, that red box there: that's the door.
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
12 Jul 2016 03:12 PM
Pairs and next are better for iterating through a dictionary, and a numeric for loop is better for iterating through an array.


Report Abuse
RangerDelta is not online. RangerDelta
Joined: 10 Jul 2016
Total Posts: 1194
12 Jul 2016 03:12 PM
It does. Neither of them are truly better. Crazyman32 wrote a good article on Medium about this. If you actually care about micro-efficiency (which you absolutely shouldn't, it's ridiculously silly), use a numerator loop instead of an iterator.


Report Abuse
alelollo is not online. alelollo
Joined: 27 Jun 2013
Total Posts: 33
12 Jul 2016 03:16 PM
Use next because it uses 2 less words than pairs (including parentheses and excluding spaces)
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
12 Jul 2016 03:16 PM
Pairs looks better.


Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
12 Jul 2016 03:21 PM
Again, they are exactly the same. If you're concerned this badly about efficiency, DON'T USE LUA. Like seriously, if you care this much, why not use something more efficient like C#?


Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
12 Jul 2016 03:35 PM
Next looks better.


Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
12 Jul 2016 03:38 PM
It's the same thing. [2]

OP pairs returns next, realistically you're iterating using next when you call pairs.
Report Abuse
TheeDeathCaster is not online. TheeDeathCaster
Joined: 19 Jun 2011
Total Posts: 1191
12 Jul 2016 03:38 PM
@Jarod Nah, I'll stay & use lua; and sheesh, why the aggressiveness? It was just a simple question. lol

I am aware that both do the same exact thing, except w/ how they're set up: for i, v in pairs(tbl) & for i, v in next, tbl.

Part of the reason for my question is because I heard pairs uses next, as the code below shows (or shows similarly? I don't remember how users set up the example.):

function pairs(tbl)
return next(tbl, 1) -- Someting like this..?
end

print(pairs({1, 2, 3})) -- 2 2


Now, look up in the upper-right corner of your screen, that red box there: that's the door.
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
12 Jul 2016 03:54 PM
pairs behaves very similar to the following

local function pairs(tbl)
return next, tbl, nil
end

so when you do
for key, value in pairs(tbl) do

The pairs(tbl) is evaluated _before the loop_ so then the loop ends up actually doing
for key, value in next, tbl, nil do
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