What's the difference between array[i] and v in ipairs loops?

Hello. I’m using ipairs to loop through a GetDescendants() array. I want to change some specific elements of it.
What is the difference of changing properties of those elements through array[i] and v?
Also, what is the difference if i use those with pairs?

1 Like

There really isn’t much of a difference, except that doing:

for i, v in pairs(arr) do
   --something
end

is a TINY bit slower than doing it with arr[i], (its so tiny that it doesnt matter in most cases). Using i, v also allows you to just get the object without having to do arr[i].

array[i] is basically the same as v, since v is the value for the specific iteration and i is the index for the iteration.

1 Like

Dont quote me on this one but I also think that ipairs will stop if it reaches a nil value and pairs wont.

Yeah, ipairs stops if it reaches a nil value. ipairs can also only be used for arrays, and it goes through it numerically (the same order as you declared the array), whereas pairs is not guaranteed to go through it in order. Pairs can be used for both arrays and maps.

1 Like

I don’t think you guys got it right, or i didn’t… let me reformulate:
what’s the difference between
for i,v in ipairs(arr) do
arr[i].Property = blablabla
end

and

for i,v in ipairs(arr) do
v.Property = blablabla
end
and same with pairs?

1 Like

yea, there isn’t really much of a difference between them.
as @nate213121 said, for i, v in ipairs(arr) is slightly slower but not slow enough to be a reason not to use it.

and yes, with ipairs, the main function of it is that it stops when it reaches nil

Yes but he also asked what happens if he does the same with pairs, which is slightly different as we stated above

are you sure that there is no difference? i don’t remember the case, but
once i tried using v.Property, and it didn’t change
but after i tried using arr[i].Property it actually changed
maybe there is difference in some specific cases?

From personal experience, there shouldn’t be any difference, unless you keep calling GetDescendants()[i] in every iteration. In this case the descendants could change, messing up the order of the loop.

(edit: this is wrong in most cases, anyone looking at this now, see this post.)

v is a copy of the value in the array. if you set v to a value, it will only change that copied variable and not the value in the array

setting arr[i] actually changes the value of the item in the array, which is likely what you want to do if you are setting individual items in an array to something else

2 Likes

Does this only count for ipairs? I always loop with pairs and I can change properties using v just fine

yeah, wanted to ask why v works too then

This isn’t quite right. There are only certain data types in Luau that are copied, others are passed by reference.

Only a number, boolean, string or nil is copied, the rest are passed by reference, that is, they are the same object.

An Instance is actually of the datatype userdata; it is a wrapper around a C++ pointer to the instance (memory location of the instance) so that Luau can interact with it. userdatas are passed by reference to an iteration, they are not copied across. Modifying it through v should actually work just fine.

That being said, if for whatever reason the userdata was copied across, the copy would still point to the same instance, and therefore modify the same instance. I’m not sure what the OP did to trigger this, as this should not be possible in a context like this one.

4 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.