c = {1,2,"a","d",c = 12, q = 20} for i,v in pairs(c) do print(i,v) end
I'm trying to understand this code. So does pairs basically turn i and v to variables of the table c in this script? I get what this script does and I get if you change the letter in parenthesis after pairs and the letter before table to the same the script still works. I just cant get myself to understand this like why would people do it like this, maybe we need two variables for the same table? since pretty much i and v equal the same thing now? I might sound a bit lost but yeah.
Update: How come when i print i by itself when theres a variable the variable letter prints. and but when i print v by it self the variable value prints
Pairs iterates over a table
I won't talk about the technicalities of pairs
because that's for people asking about how foreach loops like that work. Pairs goes through every entry in a table and returns the key and value pair at each iteration.
For example:
Say I had a table like
t = { 1,2,3, key = 'value'; key2 = true; }
And a bit of code that prints out the contents of the table like so
for k,v in pairs(t) do print(k,v) end
You can expect the output to be
1 1
2 2
3 3
key value
key2 true