how can ipairs be useful in a script
When traversing the array part of a table it might be useful to go in order. next (and therefore pairs) do not guarantee order.
For example, if you have a dictionary like this:
local Dictionary = {
["Index1"] = 1;
["Index2"] = 3;
["Index3"] = 4
}
You can go through all the elements in a for loop like this:
for index,value in pairs(Dictionary) do
print(index,value)
end
Yes, also as @sjr04 said this will not guarantee order, Index2 might come before Index1.
Now if you have an array like local array = {1,3,7,11,2} , you can use ipairs to go in order of indexes:
for index,value in ipairs(array) do
print(index,value)
end
I noticed that if there is a nil in a table, using ipairs would stop looping through the table when it hits nil, using pairs would. Maybe if someone uses nil in a table as a limit for something, it could be useful…
ipairs isn’t for Dictionaries, its for Arrays. It performs faster if you have an Array that you know has ordered keys. It will also always be in order, since it’s working with numerical indexes.
Yeah I’m sorry I should have mentioned ipairs as well.
OP isn’t asking about pairs tho
I know, I didn’t realize that until now, my bad