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

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.

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.

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

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.

Ah, I totally missed that. Thanks @davebollinger.

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.

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

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.

Ah, I totally missed that. Thanks @davebollinger.