So I have learned that in ipairs stop at nil when looping through a table, and that it is more “in order” than in pairs, but what I didn’t learn is how can it be more useful than in pairs. I haven’t thought of a single scenario in which it beats in pairs in better usage. Can someone enlighten me on this?
I think a scenario that would be good at this is like organization over something or data.
If you use the Search bar up top and search ‘pairs vs ipairs’ you’ll get a few other posts that deal with this question.
None of the posts include any usage for ipairs. They just explain the difference which I already know.
ipairs are useful for stuff like storing the order of items in a shop. I found that pairs sometimes gets the table order mixed up and using ipairs prevents this.
That’s interesting! I never thought of that. You can table.insert a table into another table and not get the order mixed up. That’s quite handy actually.
ipairs
is used primarily to iterate through arrays. You may use it in scenarios where you are looping through a mixed table and, scanning the numerical indexes in order. Or perhaps, you want to update the table’s index in correlation to another table. One use case of ipairs
is working with ordered values in a leaderboard.
Question already answered here:
Search the forum in beforehand before you post anything.