Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Do these lines do the same thing?

Asked by
FiredDusk 1466 Moderation Voter
8 years ago

I am just wondering if these both do the same thing.

I always use:

1for i,v in pairs do

I never use:

1for i = 1, #Bleh do
2
A lot of great answers below. OldPalHappy 1477 — 8y

3 answers

Log in to vote
3
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago
Edited 8 years ago

Not exactly.

The pairs function iterates over the key/value pairs in a table. With this function the key/value mappings do not have to be ordered numerically for it to look through all the pairs.

The ipairs function is more similar to a numeric for loop, as it does require numeric ordering of key/value pairs. However, it stops as soon as it finds a nil value so it may not loop over all the mappings.

Usage Examples

01local table = {"Apple", "Banana", [4] = "Donut", ["Peach"] = "Pear"}
02 
03for key, value in pairs(table) do
04    print(key, value)
05end
06 
07-- Output:
08--    1 Apple
09--    2 Banana
10--    4 Donut
11--    Peach Pear
12 
13for key, value in ipairs(table) do
14    print(key, value)
15end
View all 27 lines...
2
4 = "Donut" would error. You need to use ["4"] instead. Sir_Melio 221 — 8y
0
Whoops, my bad.  Should have run it beforehand to catch typos. BlackJPI 2658 — 8y
Ad
Log in to vote
3
Answered by
Sir_Melio 221 Moderation Voter
8 years ago

First one catches the value's integer (position) and value.

01myTable = {"Hello", 10, true, function() return "worked!" end}
02 
03for i,v in pairs(myTable) do
04    print(i, v, type(v))
05    if type(v) == "function" then
06        print(v()) -- Runs the function, returns the string and uses it in a print-function.
07    end
08end
09 
10--[[ Output:
111   Hello   string
122   10  number
133   true    boolean
144   function: xxxxxxxx  function
15worked!
16]]

The fourth value prints a bunch of characters, not necessarily just x's. It's the function's ID.

Second one uses integers to determine how many loops the for-loop should do. It also uses an identifier to help find out how many loops it has done.

01startNumber = 9
02stopNumber = 23
03 
04for i = startNumber / 3, stopNumber / 3 do -- 3, 7.666...
05    print(i * 2)
06end
07 
08--[[ Output:
096
108
1110
1212
1314
14]]

It started with 6 since 3 was the starting number and it printed 3 times 2 (print(i * 2)). It ended with 14 since 16 would mean i is equal to 8 which is above the ending number (7.666...) and could not be included in the loop.

Log in to vote
3
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
8 years ago
Edited 8 years ago

I'm gonna talk about how these actually work. Lua.org is a good place to go that.


for i = 1, #tab do is a numeric for loop. It's the same as

1for i = 1, 10 do
2    print(i)
3end

which is pretty straightforward.


for i, v in pairs(tab) do, however, is a generic for loop. It uses an iterator function. According to the wiki, an iterator function is just "a function that returns the next set of values each time it is called."

Before we talk about pairs, let's cover ipairs. It's actually pretty simple how ipairs works.

01function iter (t, i)
02    i = i + 1
03    local v = t[i]
04    if v then
05        return i, v
06    end
07end
08 
09function ipairs (t)
10    return iter, t, 0
11end

iter is the iterator function, t is the array, and 0 is the initial value. Lua then calls iter(a, 0), then iter(a, 1), then iter(a, 2) etc.


pairs() works similarly.

1function pairs (t)
2    return next, t, nil
3end

next is a primitive iterator function in Lua. next(table, key) will return the next key and value in the table, although in no specified order. next(table, nil) will return the first key and value in the table, which is why pairs also returns nil.

You can also use next directly. Since pairs(t) returns next, t, nil, we can just do this:

1for i, v in next, t, nil do

Or, since nil is nil,

1for i, v in next, t do


I think the other answer already explained the practical differences, so I'll leave it at that.

Answer this question