Well, let's look at the syntax.
Code: Select all
for i,v in ipairs(t) do
-- ...
end
i and
v are special variables (they can be called anything, but i/v is standard) that ipairs provides you with which refer to an element of the table you're looking through (in this case, t). i is the index of the element, and v is the element itself. So something like
Code: Select all
t = {"hi", "bye", 42}
for i,v in ipairs(t) do
print(i..": "..v)
end
would output
v, then, is equivalent to t
. It's just a convenient shorthand because generally when you're iterating through a table you want to do things with the table's contents. Thus, you could rewrite the code in your post as:
Code: Select all
for i,v in ipairs(h) do
print(v)
end
Note that ipairs will iterate over every
consecutive numeric index in the table, from 1 to wherever. So if your table's like this:
Code: Select all
t = {}
t[1] = "foo"
t[2] = "bar"
t[3] = "baz"
then running the table printing code above would output
But if the table looked like
Code: Select all
t = {}
t[1] = "foo"
t[2] = "bar"
t[4] = "honk"
then the output would look like
because 4 does not come after 2. If you then added
4 would be part of the chain again, since 3 comes after 2 and 4 comes after 3. Thus, the output would be