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

Why is ipairs preferred over pairs when iterating over an array?

Asked by
Discern 1007 Moderation Voter
8 years ago

I've just been reading the Wiki page on Tables, and when I read the section about iterating over a table, it says:

You should use ipairs to iterate over arrays, rather than pairs.

Why is this true?

0
It's useful for stopping when a nil value arises in the array. It also depends on what you want. legobuildermaster 220 — 8y
0
Yeah. But pairs is more generic. I'd rather use an if statement to skip over a nil value in an array than use ipairs. CodingEvolution 490 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

I'm going to answer this the best I can

I'm not that good with arrays, but I can provide a reason for this nonsense. You see, iPairs is also used because it does this in an Order, and sometimes Order is preferred in tables. That's one of the reasons iPairs is used instead of Pairs. iPairs does it in an order, Pairs does not.

iPairs also stops when a nil value is returned. Sometimes this is useful for some scripts, sometimes it is not. It is a reason why I personally, and many people prefer Pairs over iPairs.

###########Pls don't hurt me, I was reading forums and encountered this.
Ad

Answer this question