Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

IIRC, the right way to check if a table is empty is

    next(mytable) == nil
As the sibling comment points to, the documentation of the # operator is complex. It returns a border which is defined as:

    a border is any positive integer index present in the table
    that is followed by an absent index, plus two limit cases:
    zero, when index 1 is absent;
    and the maximum value for an integer, when that index is present.
    Note that keys that are not positive integers
    do not interfere with borders.
When the table has multiple borders, the behavior is undefined.

     lua         
    Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
    > print(#{nil, 20})
    0
    > a = {10, nil}
    > print(#a)
    1
    > a[-1] = -10
    > print(#a)
    1
    > a[1] = nil
    > print(#a)
    0


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: