lua-users home
lua-l archive

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


2016-03-16 13:10 GMT+02:00 Peter Aronoff <telemachus@arpinum.org>:
> On Wednesday, March 16, 2016 at 10:21AM, Dirk Laurie wrote:
>> Lua's table type is so general and versatile that a bit of discipline in
>> its usage does not come amiss. For most of my tables, I follow the
>> following rules.
>>
>> 1. The table is a proper sequence (§3.4.7 of the Lua 5.3 manual).
>> 2. The [0] entry, if present, is an index to an arbitrary selection of
>>    keys, usually a string with pieces gmatching "%a%w*" but sometimes
>>    just a list of values.
>> 3. I seldom use 'pairs'.
>
> I think I see the use of (1) and (3), but what's the point of (2)?

Usually tbl[0] is just a blank-separated concatenation of the string
keys in some logical order, such as alphabetic, so that

   for key in tbl[0]:gmatch"%a%w*" do
     local value=tbl[key]
     ...
   end

traverses the table in a deterministic way.