löve2d #array not returning last integer index of an array

Issue #1250 invalid
Robin
created an issue

Vanilla Lua code (new empty project):

local array = {1,2,3,4,5}
array[3] = nil
array[4] = nil
print(#array) -- returns 5

Löve2d code (new empty project):

function love.load()
    local array = {1,2,3,4,5}
    array[3] = nil
    array[4] = nil
    print(#array) -- returns 2 (bug?)
end

Is it how it should work or is it a bug?

Lua wiki says: The # operator doesn't count all the items in the table (!). Instead it finds the last integer (non-fractional number) key.

So I think it is a bug but I'm not sure as I am fairly new to Lua and Löve2d.

NB: also setting only 1 value to nil returns 5 in Löve2d.

Comments (2)

  1. Bart van Strien

    This is valid behaviour according to the reference manual (version 5.1). The difference is probably explained by the fact we're using luajit, which is a reimplementation of lua 5.1 (with some 5.2 features).

    From the reference manual:

    The length of a table t is defined to be any integer index n such that t[n] is not nil and t[n+1] is nil; moreover, if t[1] is nil, n can be zero.

  2. Log in to comment