for k,v in pairs, what does the k do?

Okay, so I have been doing c# and stuff, learning programming, so I was looking at Lua code, and this is what I dont get



for k, v in pairs(player.GetAll()) do
   Msg( v:Nick() .. "
")
end


So, as far as I can see, it gets the list of players, then for each player, it get’s it’s name, so it goes
player > v = player > print v’s name

But, why is the k just chilling out, can we remove it? Why does it exist, I have been looking and apparently it’s something to do with tables, which I have also found that Lua loves tables,

I read this thread: http://forum.facepunch.com/showthread.php?t=944419
But I’m still a bit confused

(I’m thinking of making a little basic gamemode, and instead of saying how to do this and that, work out the syntax and stuff of Lua and work it out myself)
Also, http://www.lua.org/pil/4.3.5.html makes sense, but I dont understand why we need it in the example above…

The k is the index of the pair. An example of using it would be to look up the value from another table related to the first one.



local table1 = {"is this a test?", "do you like blueberrys?"}
local table2 = {"yes", "no"}

for k,v in pairs (table1) do
   print (v..table2 [k])
end


Output:

is this a test?yes
do you like blueberrys?no

I would try and explain it further but I have class in 10 minutes and writing this from my phone.

let’s say we have a table named ‘AnonTable’, let’s also assume at time of traversal the table has the values “yes”, and true in it. This is how the table is structured.
[lua]
–AnonTable[key] = value
AnonTable[1] = “yes”;
AnonTable[2] = true;
[/lua]

Now, knowing how our table is structured, we can take our table and use a ‘for-in loop’ to spit out the table key and value.

[lua]
for key, value in pairs( AnonTable ) do
print(key, value);
end
[/lua]

This would print
1 yes
2 true
into your console. See, tables in lua contain an identifying key, matched with it’s partner value.

Ohh that makes sense now, what i was reading was confusing to me, and didnt explain much for what it did, but it makes sense now, thanks c-unit and Alig96 for the help :slight_smile:
Very much appreciated

the key doesn’t need to be a number to work with pairs though, does it? correct me if i’m wrong

pairs will iterate through every entry in a table, even if the key was a function or anything else (which is possible in Lua). Additionally there is ipairs which starts iterating at 1, and keeps incrementing by 1 until it hits an undefined value:
[lua]
tbl = {}
tbl[1] = true
tbl[2] = true
tbl[4] = true – ipairs won’t reach this because there is no 3
tbl[“string”] = true – ipairs would never reach this, but pairs will.
tbl[function () end] = true – this will also work with pairs, but not with ipairs.
[/lua]

K stands for Key, V stands for Value.

Table basically looks like this:
[lua]table[key] = value[/lua]

For example:
[lua]Food[1] = “Orange”
Food[2] = “Apple”
Food[3] = “Mango”[/lua]

If you run a loop throught it, k will be a number between 1-3 and value will be either Orange, Apple or Mango

Thank you everyone, I understand it now, it makes alot more sense :D, A big thanks to everyone, it is very much appreciated

Furthermore, you don’t have to do k,v. You can do any variable name and it will work.
The first variable will always be key, the second variable will always be value.
If, for instance, you were doing a loop within a loop, you would do something like this:
[lua]
for key,subtable in pairs(tbl)do
for key2,item in pairs(subtable)do
print(item)
end
end
[/lua]
That would iterate through a table comprised of SUBtables, like this:
[lua]
tbl = {
a = {
hi = 2,
there = 3
},
b = {
whats = 4,
up = 5
}
}
[/lua]

I prefer to use k2, v2 when looping through subtables, feels more organized tbh

I do too but in this case I wanted to show that it could be just about anything.

Or just name them properly. So if you’re looping through a table of names, use name instead of v.

table[key] = value

k is the key, v is the value