Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Why is pairs the socially correct way to do a table loop?
Started by harrisonfox8 Apr 16 2018 12:12 PM

- - - - -
4 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

harrisonfox8

[GLOBAL: userInfoPane.html]
harrisonfox8
  • Observer

  • 1 posts
  • Corona SDK

Why is pairs the socially correct way to do a table loop?

pairs(foo) returns next and foo

next, foo is next and foo

Why is “for i,v in pairs(foo) do” considered more correct than “for i,v in next, foo do”? “next, foo” is the same except it doesnt require a pointless function call.

[TOPIC: post.html]
#2

carloscosta

[GLOBAL: userInfoPane.html]
carloscosta
  • Contributor

  • 613 posts
  • Corona SDK

I think the key difference is this:

 

 

 

The order in which the indices are enumerated is not specified, even for numeric indices. To traverse an array in numeric order, use a numerical for loop or the ipairs() function.


[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,484 posts
  • Corona SDK

There is no 'socially' correct way to iterate a table.

Tables are indexed in different ways and thus there are different ways to iterate (over) them.

1. Numerically indexed table indexed numerically and in-order
local data = { "a", "b", "c" }
data[#data+1] = "d"

for i = 1, #data do
   print( i, data[i] )
end

2. Numerically indexed table indexed numerically iterated numerically in order with ipairs()
local data = { "a", "b", "c" }
data[#data+1] = "d"

for i,entry in ipairs(data)do
   print( i, data[i], entry ) -- entry and data[i] are the same
end

3. Numerically indexed table iterated in some random order with pairs()
local data = { "a", "b", "c" }
data[#data+1] = "d"

for k,v in pairs(data) do
   print( k, data[k], v ) -- k is the index/key, v is the value (same as data[k]
end

4. Non-numerically indexed tables can only be iterated with pairs()
local data = {}

local obj = display.newCircle(10,10,5)
obj.name = "bob"
data[obj] = obj

local obj = display.newCircle(30,10,5)
obj.name = "bill"
data[obj] = obj

local obj = display.newCircle(50,10,5)
obj.name = "sue"
data[obj] = obj


for k,v in pairs(data) do
   print( v.name ) -- prints name of object (bob, bill, or sue)
end

Edited by roaminggamer, 03 August 2018 - 09:33 AM.


[TOPIC: post.html]
#4

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,296 posts
  • Enterprise

i believe OP is specifically asking about iterating over a keyed table, asking:  is pairs() preferable to next()?

 

pairs() is often preferred simply because it's easier and cleaner - it's literally just a wrapper around returning next and the table.  next() might give some trivial performance advantage over pairs() in certain circumstances, but it's not going to be enough to matter for most use cases.  (and certainly nowhere near the difference between keyed and indexed iteration)

 

the OP appears to believe that pairs() is a "pointless function call", presumably because they think that "next" doesn't imply a function call also (given the syntax you just pass a reference in the loop), but rest assured that next will be called as a function also.



[TOPIC: post.html]
#5

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,484 posts
  • Corona SDK

Ah, I totally missed that. Thanks @davebollinger.




[topic_controls]
[/topic_controls]