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

Good explanation of for i,v in pairs?

Asked by 4 years ago

I need a great explanation of

for i,v in pairs

1 answer

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

This is another form of what we call a loop. A loop is a sequence of instructions that is continually repeated until a certain condition is reached, some examples of these include while and for loops.

However, this loop is special, and is formally recognized as a pairs loop. This is a special form of iteration designed to traverse through an array. The pairs() function will push the current index point, and element within the table at the respective point of iteration into two variables, i and v. These define Index and Value.

Let's take a look at an example of how this would operate by creating an arbitrary array of data:

local myTable = {1,2,"Hello!", true, nil}

Each item within myTable is recognized as an individual element, if we wanted to go over these elements and modify, or analyze them, we'd apply a pairs loop.

for i,v in pairs(myTable) do
    print(i,v)
end

In our output, we recieve:

--[[

    Index:  Value:
    1       1
    2       2
    3       "Hello"
    4       true
    5       nil 

]]

Let's see another example of a potential application with a pairs loop. In this program, our goal is to dive into an array, analyze it's data, and add it one-by-one to another table.

local myTable = {"Hello", "world!"}
local myTable2 = {}

for i,String in pairs(myTable) do
    table.insert(myTable2, i, String)
end

print(table.concat(myTable2, ' ')) --> "Hello world!"

Hold up! Some things look a bit odd here! Let me explain. Firstly, i and v behave identically to parameters, since they are! We can change the name to whatever we wish, and the data involved will not be affected.


We are also using some interesting functions here, what do they do?

table.insert allows us to push a new element into an array, it takes a total of three arguments—with the second one as optional. These include: The array we're modifying, the index placement of the element to be inserted (where in the table do we put it), and finally, the actual data. table.insert by default will automatically place the data within the first index, so since we wish to copy the array above identically via a pairs loop, we have to make sure the data goes in the right places.

table.concat, otherwise known as table.concatenate, will take all the data within the given table and compress it into one strand, we supplied a secondary argument ' ', which tells it how it will format the conjunction of each element (one space apart).


An advanced note:

Sometimes we don't want to use a parameter provided to us, and a lot of the time Scripters will still leave it as an open datapoint for Lua to load information into, which takes up unnecessary processing. So, we can use the special character `_— underscore—to tell Lua that we want to ignore said information.


Let's take a look into a final approach, of a real-life application with pairs. For this example, we'll iterate through each Player of a game, and kill them.

local Players = game:GetService("Players"):GetPlayers() --// Gives an array.

for _,Player in pairs(Players) do
    if (Player.Character) then
        local Humanoid = Player.Character:FindFirstChildOfClass("Humanoid")
        if (Humanoid) then
            Humanoid:TakeDamage(Humanoid.Health)
        end
    end
end

I hope this gives proper insight into potential applications of a pairs loop, as well as what you were hoping to learn! If you have anymore questions, go ahead and drop a comment below.

1
Wow! Very specifically written! No wonder your reputation is over 3000! blarp_blurp645 63 — 4y
0
Thanks;) Ziffixture 6913 — 4y
Ad

Answer this question