#im new to scripting, can anyone tell me what "i, v in pairs" does?

10 messages · Page 1 of 1 (latest)

magic warren
#

I, v in pairs goes through a table I acts as our index so for ex 1=obj1, 2=obj2 while value would be the actual value obj1, obj2

toxic wren
#

It exexutes things for all the variables in a table

#

I = index
V = Value

#

Don't really know where to use i but v is useful

magic warren
#

Also @toxic wren you do not need pairs or ipairs

jolly quiver
#

do u guys release that he is new

#

and you have to explain it newie

#

he doesn't even know what an index is

frosty robin
#

pairs is a built in function that iterates over a given table. You can use it in a for loop to iterate over the table you want. The "i" and "v" are variables that are assigned to the Key and the Value of the current element.
For ex:

local myTable = {key1 = "value1", key2 = "value2", key3 = "value3"}

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

This will print "key1, value1", "key2, value2", "key3, value3".

The key of a table is like a variable where you store a value. It can be a number, a string, etc. When you make a table like this:

local myTable = {"value1","value2","value3"}

The language assigns numeric keys to the values.
So if you do:

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

It would print "1, value1", "2, value2", "3, value3".

magic warren