# in pairs

I can’t wrap my nubile brain around what “in pairs” means in a for loop when dealing with a table. I’ve checked the documentation and am still terribly confused. Can anyone help? Possibly explain it with hand puppets?

Toward the bottom of this

https://coolcodea.wordpress.com/2013/04/07/26-tables/

@dComposer a for loop is an integer based iterator, a for in pairs loop is a key based iterator.

For example:

``````--tbl is the table we want to iterate over
local tbl = {5,6,3,2,6,8,9,0,1,2}
for n=1,10 do
local v = tbl[n]
-- v is the n part of your table, when n is 2 v = 6, when n is 8 v = 0 etc
end
``````
``````local tbl = {}
tbl[32] = "hello"
tbl[45] = "world"
tbl[61] = "!"

for k,v in pairs(tbl) do
--if your table is indexed with an integer then k will equal the integer key
in ascending order in tbl, in this case 32,45,61

--v is the same as tbl[k], it is the value part of
the pair that makes the (k)ey and (v)alue.
end

--in this example you can iterate over all values in a table and at the same time get the key for each one, you can use strings in as your key like this.

tbl = {}
tbl["Hello"] = 40

--if you were to use a for k,v in pairs loop on this table then, k = "Hello" and v = 40
``````

Hope that clears it up.

Thank you both so much! Crystal clear and no hand puppets were even needed!!