Hello!
I'm fairly new at scripting, and I've recently learned about the for loop.
Of course, for i,v in ... is a common form of this loop, and I've seen three variations of it so far.
for i,v in pairs
for i,v in ipairs
... and
for i,v in next
I've tried looking at the ROBLOX Wiki articles, but I can't seem to understand. I do know what tables are and their use, if that helps.
for a,b in pairs
is a common loop.
It's entire syntax is- for a,b in pairs
(table) do.
In basic terms, what this does is goes across every value in a table and will reference it as you've put forth in the loop.
I
is the number of times it has ran, in a sense.
V
is the object it's referring to in the table.
In fact, the reason why the two most common variables for it are i and v, is because they stand for index and value. Indexing how many times it has ran, and running through each value. Keep in mind you can use any value for i and v; I instinctively use a and b.
Here's an example:
x = {"Hello", "How are you", 666} -- declaring our table for a,b in pairs(x) do print(b) end
This would run the loop three times, for each value in the table.
Since it's running three times, each time, referencing b
as the value in the table it's running with, and you can print both string values and number values, it will print-
Hello How are you 666
This is the basis of it.
Now, to answer your question in full, I'll tell you the difference between for a,b in ... pairs, ipairs, and finally, next.
ipairs
does the exact same thing as pairs, but with a slight twist to it.
ipairs runs through the table, until it finds a nil
value, or a value that is non-existent, if that makes sense. So, if you ran the script I showed you for pairs, but just replaced pairs with ipairs, it would do the exact same thing, however...
Let's give you an example of what ipairs will do when it comes across a nil value.
y = {"Hello","Are you feeling good?",nil,"Yay!"} -- declaring our table again for a,b in ipairs(y) do print(b) end
... that would print out:
Hello Are you feeling good?
Why? Because it's stopping at nil. It will not print nil, because you can't print a value that isn't there, and will not print "Yay!", either, because it has stopped.
Finally, next
.
z = {"A","B","C"} print(next(z))
This returns...
1 A
It returns both the index and it's associated value.
print(next(z,2))
3 C
Wait. Why C? Isn't it the third value? Well, logically, yes. But it counts A as 0, B as 1, and C as 2.
However, if you were to go like this,
print(next(z[2]))
... it would return-
2 B
... because it's directly referring to the second value of the table, and it doesn't have a second argument.
I hope this helped! Good luck; and please consider giving me an upvote!
pairs
, ipairs
and next
are the built-in global functions used in generic for loops for indexing through tables.
pairs
function:
local t={'a','b','c','d','e','f',a=1,b=2,c=4,d=8,e=16,f=32} for _,v in pairs(t)do print(_,v) end
Output:
1 a 2 b 3 c 4 d 5 e 6 f d 8 e 16 f 32 a 1 b 2 c 4
pairs
has no particular order for indices that are non-numerical.
ipairs
function:
for _,v in ipairs(t)do print(_,v) end
Output:
1 a 2 b 3 c 4 d 5 e 6 f
ipairs
does not index non-numerical indices at all.
next
function:
for _,v in next,t do print(_,v) end
Output:
(same output as pairs)
Calling next(t)
will tell you whether or not the table is empty.
The generic for loop can also be used with your own functions:
for v in function(_,n) return(n or 0)+1 end do print(v) end
This will count up forever.
The numeric for loop has one variable and two or three arguments:
for variable=start,end,increment do
ex.
for i=0,6,2 do print(i) end
Output:
0 2 4 6
pairs
iterates over all indices, even if they aren't positive whole numbers or in order.
for i,v in ipairs(t) do
will function the same as for i = 1,#t do local v = t[i];
.
If the table is simply a list, there is no difference between the behavior of pairs
and ipairs
.
If the table is not a list, most likely you want to use pairs
so that you can traverse the non-list parts of it.
Remember that ipairs
will also only traverse the elements counted in the table length (#t
) so these examples don't print out all of the values stored in the table:
local one = {}; one[0] = true; one[1] = true; one[2] = true; for i,v in ipairs(one) do print(i); end -- Output: -- 1 -- 2 local one = {}; one[1] = true; one[1.5] = true; one[2] = true; for i,v in ipairs(one) do print(i); end -- Output: -- 1 -- 2 local one = {}; one[1] = true; one[2] = true; one[4] = true; one[5] = true; for i,v in ipairs(one) do print(i); end -- Output: -- 1 -- 2
Locked by Goulstem
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?