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

Difference between pairs, ipairs, and next? [closed]

Asked by 11 years ago

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.

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?

3 answers

Log in to vote
10
Answered by 11 years ago

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:

1x = {"Hello", "How are you", 666} -- declaring our table
2 
3for a,b in pairs(x) do
4 
5print(b)
6 
7end

This would run the loop three times, for each value in the table. Since it's running three times, each time, referencing bas the value in the table it's running with, and you can print both string values and number values, it will print-

1Hello
2How are you
3666

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.

1y = {"Hello","Are you feeling good?",nil,"Yay!"} -- declaring our table again
2 
3for a,b in ipairs(y) do
4 
5print(b)
6 
7end

... that would print out:

1Hello
2Are 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.

1z = {"A","B","C"}
2 
3print(next(z))

This returns...

It returns both the index and it's associated value.

print(next(z,2))

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-

... 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!

Ad
Log in to vote
4
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
11 years ago

pairs, ipairs and next are the built-in global functions used in generic for loops for indexing through tables.

pairs function:

1local t={'a','b','c','d','e','f',a=1,b=2,c=4,d=8,e=16,f=32}
2for _,v in pairs(t)do
3    print(_,v)
4end

Output:

011   a
022   b
033   c
044   d
055   e
066   f
07d   8
08e   16
09f   32
10a   1
11b   2
12c   4

pairs has no particular order for indices that are non-numerical.

ipairs function:

1for _,v in ipairs(t)do
2    print(_,v)
3end

Output:

11   a
22   b
33   c
44   d
55   e
66   f

ipairs does not index non-numerical indices at all.

next function:

1for _,v in next,t do
2    print(_,v)
3end

Output:

1(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:

1for v in function(_,n)
2    return(n or 0)+1
3end do
4    print(v)
5end

This will count up forever.

The numeric for loop has one variable and two or three arguments: for variable=start,end,increment do ex.

1for i=0,6,2 do
2    print(i)
3end

Output:

10
22
34
46
Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
11 years ago

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:

01local one = {};
02one[0] = true;
03one[1] = true;
04one[2] = true;
05for i,v in ipairs(one) do
06    print(i);
07end
08-- Output:
09-- 1
10-- 2
11 
12local one = {};
13one[1] = true;
14one[1.5] = true;
15one[2] = true;
View all 33 lines...