|
| 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 |
|
|
| |
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 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
|
  |
| 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 |
|
|
| |
|
|
| 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
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
| |
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 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 |
|
|