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

Difference between "pairs" and "ipairs"? [closed]

Asked by 6 years ago

So i very recently learned the for i,v in pairs loop and ive seen some people doing "for i,v in ipairs" instead of "for i,v in pairs"? What's the difference?

Locked by User#19524

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
4
Answered by 6 years ago

So basically for i,v in pairs will go through an entire table, and do whatever it's asked to. But for i,v in ipairs has a twist. It stops when it reaches a nil value. For example:

local myTable = {1,2,nil,3}
for i,v in pairs(myTable) do
    print(v)
end

This would output:

1
2
3

But if we did:

local myTable = {1,2,nil,3}
for i,v in ipairs(myTable) do
    print(v)
end

Would only output:

1
2

Because it stops at the nil value

Ad