lua-users home
lua-l archive

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



On Oct 2, 2013, at 1:21 PM, liam mail <liam.list@googlemail.com> wrote:


On 2 October 2013 20:54, Tim Hill <drtimhill@gmail.com> wrote:
No, I'm going to have to check it's an array first, and then iterate it

Why?
Use a numeric for as has been mentioned already in this thread. You are only interested in the int keys, so why care if there is anything else in the table? It is perfectly valid to have, for example, string entries in a table and for the table to still contain a sequence.

This isn't about non-integer keys, I perfectly understand (and use) hybrid tables that have both an array and additional information in name/value pairs. It's about arrays that, for a users perspective, have "holes". Lua doesn't allow this, and it's behavior in such circumstances is counter-intuitive (witness the frequency of posts about the # operator).

And *because* users get confused about this, I wish to be very defensive when it comes to writing APIs that *do* expect a well-formed sequence (with or without auxiliary non-integer keys). Before just blindly generating a (possibly erroneous) result, I want to validate the sequence, which in effect means looking for holes (or, more correctly, answering the question: "Does a table with N positive integer keys have all such keys in the range 1..N" which is in line with the strict Lua definition of a sequence). And Lua does not provide an efficient means to do this. That was the point of my post.

--Tim