lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thu, Oct 15, 2020 at 6:11 PM 孙世龙 sunshilong <sunshilong369@gmail.com> wrote:
> Why does the output of "for in pairs" is right whereas the output of
> "for in ipairs" is wrong?

The question is "Why, when I give ipairs a table that has a nil value
at t[2], does it stop iterating? And why, when I give it a table that
has a nil value at t[1], does it not even iterate over one entry?"

The manual says:
ipairs (t)
Returns three values (an iterator function, the table t, and 0) so
that the construction
     for i,v in ipairs(t) do body end
will iterate over the key–value pairs (1,t[1]), (2,t[2]), ..., up to
the first nil value.
https://www.lua.org/manual/5.3/manual.html#2.2

It is often helpful to read the manual. Lua is a small language and
the manual is small as well.