[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: foreachi and artificial tables
- From: Dave Bollinger <DBollinger@...>
- Date: Sat, 7 Aug 1999 18:54:48 -0400
Mike Goodey wrote:
>> would be difficult to come up with a scheme for lua to "ask"
>> for all the text indices.
I'm still a novice at Lua myself, but I think I can address this part...
It wouldn't be difficult to do so, it would be impossible. I've
stumbled on this concept a number of times myself. (too many times!) To
Lua, there is no difference between an index which doesn't "exist" (in the
classical sense of being declared) and one which "exists" (has been
referenced) and contains the value Nil - they both act the same. The docs
for the next() function explain it, and since foreach() is (essentially)
built around next() it works the same way. (getn() and thus foreachi()
have similar behaviour)
Aside: without a value present in an index, you'd have to check every
one of the zillion possible permuations that can make up a legal index name
and see if it returns a non-nil value from your C tag method and build up a
list of "used" indices. But it'd probably take a week to run. <grin>
>> Is there a way of getting this scheme to work with foreachi,
Here's a thought: the docs show how to define foreachi() in Lua itself.
Do that, and notice that it depends on getn() to work correctly. Now all
you have to do is redefine getn() in either C or Lua so that it returns the
proper value for YOUR tables. Once getn() works, the Lua version of
foreachi() ought to just start working correctly automatically! :-)
(the tag methods should still be called appopriately to retrieve the table
data, since foreachi() is implemented in Lua)
Of course, disclaimers of my novice-hood apply, as this is untested.
But it seems right.