Can I force Lua's table indexing to start from zero?

Working with 0-indexed arrays is actually pretty simple:

local array={
[0]="zero",
    "one",
    "two"
}

for i=0,#array do
    print(array[i])
end

You can use #array without subtracting 1 because the length operator actually returns the highest index (technically, the key before the first nil), not the actual "length" (which wouldn't make sense in Lua anyway).

For string operators, you'll probably have to just create duplicate functions (though there might be a better way)

ipairs() also only supports 1 indexing, so you'll have to define your own function or just use a regular for instead.


I know this question is already 1 year old, but I thought future seekers would be interested in the fact, that CFF Explorer contains a scripting language (Lua with patches), which has 0-indexed table patch:

http://www.ntcore.com/files/cffscriptv2.htm

Also, in the document above, the author stated that he had to disable most of the standard library functions, because they're incompatible with 0-indexed arrays, so please reiterate your thought process about this issue :)


I think that that Lua already has the feature that you need to make it 0-based. Unfortunately the feature that I refer to is Lua's open source license.

I was unable to find a patch or fork of Lua that changed the 1-based nature of the language.

Unfortunately forking Lua to change it to 0-based would also break backwards compatibility. Loss of all the current add-on modules may be too great a price to pay for ease of use.

Tags:

Indexing

Lua