I have currently trying to learn in pairs() but it’s confusing and I can’t really tell when to use it or what its purpose is used for. Can someone help me understand it in a better way?
Pairs can go through a directory of items instead of just number to number.
For example:
for I,v in pairs(game.Players:GetPlayers) do
if v.Name == "iceking_gfx" then
print(v.Name.." is cool")
end
end)
pairs() is used for iterating over dictionaries; ipairs() is used for arrays.
For example:
local dictionary = {
["hi"] = "yes";
["no"] = "bye"
}
for k, v in pairs(dictionary) do
if k == "hi" then
print(v)
end
end
local array = {
"yes";
"hi"
}
for i, v in ipairs(array) do
print(i, v)
end
pairs
is typically used to iterate over a table or a dictionary. It returns 2 values, the index at which the loop is currently in, and the value that index contains. For a dictionary, instead of an index, it is usually called a key.
Example for table
local fruits = {"Chicken", "Beans", "Apple"}
for i, v in pairs(fruits) do
print(i, v)
end
--Output
1 Chicken
2 Beans
3 Apple
Dictionary example
local dictionary = {
["Name"] = "Bob",
["Age"] = 576368
}
for key, value in pairs(dictionary) do
print(key, value)
end
--Expected output
Name Bob
Age 576368
The use case is varied, usually if you want to do something to each entry in a table, say you have a table of numbers and you want to add t hem all together to get a sum, you’d loop through it. Same applies for dictionaries. If your use case ever dumbs down to needing to loop through a table or dictionary, you’ll definitely need the use of pairs
For when you put for i, v
is the v a certain thing that I would have to use every time I would like to use a pair, or could I rename it to do something different?
i
and v
are the typical placeholder names that developers use for the 2 returns, i
is just short for index
and v
is short for value
, they don’t have to be named that, you can name them whatever you want, I could change one of my examples to
local fruits = {"Chicken", "Beans", "Apple"}
for taco, CHEESE in pairs(fruits) do
print(taco, CHEESE)
end
And it would still work fine, although it is recommend you name them appropriately given the context, if you’re looping through a table of players, it’s best you name the second return to player
to show yourself and other readers that you’re getting a player from the table
To Put it into simple words, Pairs() is normally used in a loop like this:
for index, player in pairs(game.Players:GetPlayers()) do
print(player.Name)
end
pairs is used to loop through a table basically.
So pairs can only be used if you have a table?
Basically yea, pairs
can also be used for dictionaries as well as stated before
Can it also be used for arrays?
woah he kinda old
Aren’t Arrays kind of like tables? They should also work with pairs if it can be iterated over
That’s cause he discovered immortality
It can also be used for this:
for i,v in pairs(instance:GetChildren()) do
--other code
end
GetChildren() Returns a table like i’ve said.