[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: More on table iteration and the Reference Manual.
- From: David Jones <drj@...>
- Date: Thu, 26 Jun 2003 12:56:43 +0000
I've been reading more of the Lua 5.0 reference manual. A fine work by
the way despite this and my previous minor niggles.
The function ipairs in the base library (section 5.1) should make the
- the function will not work unless the global variables "ipairs" is
bound to the ipairs function as it is when the base library is opened.
- ipairs _does_ support table modification during the iteration.
- ipairs uses rawgeti to access the table t (so won't work with some
tables that are emulated using metatables).
The description of the function pairs should make it clear that it
really does use the global "next" and hence will fail if next is
How about the following text:
Note that unlike <code>pairs</code> it is safe to modify the table
<code>t</code> during the iteration. At each iteration step the next
integer is generated and is used to index the table, the iteration
proceeds if an entry is found, and terminates if no entry is found.
There are a couple of implementation details which affect the precise
behaviour of this function. The first is that this function makes
reference to the global variable <code>"ipairs"</code>, hence it will
not work if you do something like <code>base.ipairs = ipairs; ipairs =
nil</code> and then subsequently use <code>base.ipairs</code>. The
second is that <code>ipairs</code> indexes the table <code>t</code> by
using <code>lua_rawgeti</code> and so ignores any metatable that
<code>t</code> might have.
Note that this function finds the <code>next</code> function by using
the global variable <code>"next"</code> and hence will fail if
<code>next</code> is redefined.