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

What does ipairs, i,v in pairs, and pairs mean?

Asked by 6 years ago

What would these statments be used for and how would I go about using them?

0
What was the point of that link crypt100 20 — 6y
0
Nvm that kinda helped crypt100 20 — 6y

1 answer

Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

What are those terms?

Those words that you pointed out are used in iterating a table. Those syntaxes will commonly be used when using a generic for loop.

What is i,v and in?

v is short for value. i is short for index. Let's take a look at a generic for loop

1local tbl  = {5,10,"hi"} -- We need a table to iterate
2for i,v in pairs(tbl) do -- I will explain pairs() in more detail. For now, you need to know that you need this function for using generic for loops
3    -- code --
4end

i is our index. You may have seen this letter in a number for loop

1for i = 1,5,1 do end -- i is our counter

i is technically a counter. It says the index of the value we are on

v is the value the generic for loop is on.

EDIT: i,v can be changed to anything(i.e hi,lol; yes,no etc.; however, it is nice to use i,v(index,value) or k,v(key,value) since they are usually the most common letters to use when working with generic for loop iteration)

Let's make a simple generic for loop that prints the values.

01local tbl  = {5,10,"hi"}
02for i,v in pairs(tbl) do
03    print("Current index",i)
04    print("Current value",v)
05    print() -- Just to make the iteration result looks more clean
06end
07--[[
08    Output:
09 
10        Current index 1
11        Current value 5
12 
13        Current index 2
14        Current value 10
15 
16        Current index 3
17        Current value hi
18--]]

in is technically a syntax you need to type down. I don't think you should really care about its role in a generic for loop. I think in is just a syntax that has something to do with what studio has behind the scenes.

What is pairs?

As you might have noticed, pairs() is one of the built-in functions. pairs() is a function that returns the next() function and the table you are iterating.

Let's take a look at our last example

1local tbl  = {5,10,"hi"}
2for i,v in pairs(tbl) do
3    print("Current index",i)
4    print("Current value",v)
5    print()
6end

pairs()'s argument is the table it needs to iterate. The pairs() function iterates a table without caring if something is nil or not. This leads me to the next iterating function, ipairs().

What is ipairs()?

ipairs() is another built-in function like pairs()

It returns the function next(), the table you are iterating, and an integer. This integer returns if the value the generic for loop is on, is nil or not(I am not quite sure though, so don't quote me on that). ipairs() stops the for loop when it encounters a nil value

01local tbl  = {5,10,nil,"hi"}
02for i,v in ipairs(tbl) do
03    print("Current index",i)
04    print("Current value",v)
05    print()
06end
07--[[
08    Output:
09        Current index 1
10        Current value 5
11 
12        Current index 2
13        Current value 10
14 
15--]]

ipairs() can be useful when you want to make sure every value is not nil value and you are not sure about it.

NOTE: There is another way to iterate a table without using pairs() and ipairs(). This will do the same thing

1local tbl  = {5,10,"hi"}
2for i,v in next,tbl do
3    print("Current index",i)
4    print("Current value",v)
5    print()
6end
0
One big note on your answer is that it does not have to be i, v. It could be for bob, billy or for lava, idiosyncrasies. It does not matter. i,v is just more common. User#21908 42 — 6y
0
Yeah, you can change it to anything saSlol2436 716 — 6y
Ad

Answer this question