What is the use of
_,i in pairs
I have always heard that term and I do not know what it's Purpose, and its ability.
What is the use of
_,i in pairs
I have always heard that term and I do not know what it's Purpose, and its ability.
It is used to iterate a table, or go through every single value inside the table.
I personally do not use it, choosing instead to use next(it works a different way), but it depends on your style.
Example:
for Index,Object in pairs(Workspace:GetChildren())do
print(string.format(
'%q is the %dth child of Workspace.',
tostring(Object),
Index
));
end;
Workspace:GetChildren() returns an array of instances that are direct descendants of Workspace.
Index is the key that is held in direct linkage to the table, with Object being the value. In this case, Object is the instance that's parent is Workspace.
Another Example:
local Friends={
['xxProMLGxx'] = 'a l33t hax0r',
['TheContinentOfAmerica'] = 'your brother',
['Shedletsky'] = 'the ROBLOX Creative Director',
};
for Username, Description in ipairs(Friends) do
print(Username..' is '..Description..', and is a friend of yours, no?');
end;
The Friends array was a dictionary that's keys were the usernames of some players and values were their descriptions.
Username was the index, which was the player's username, and Description was the phrase associated with the username.
xD Thanks bro.
What's the difference between "next" and "pairs"?
This discussion is preserved as it was. New answers, comments, and votes are closed.
1 comment
Duplicate. Remember to use the search bar.