0

I'm currently creating a small chemistry application for my TI-Nspire CX (supports Lua scripting, though, very limited and restrictive), and have run into a daunting issue. I'm attempting to update drawState.currentIndex, relying on #drawState, but #drawState == 0 despite it having 3 tables within it.

Here's my code,

local drawState = {
    currentIndex = 2,
    table = {   draw = true,
                focus = 'none',
                drawFocused = false,
            },

    menu =  {   draw = false, }
}

...

-- Called whenever enter key is pressed on calculator
function on.enterKey()
    if (drawState.currentIndex < #drawState) then
        drawState[drawState.currentIndex].draw = false
        drawState[drawState.currentIndex + 1].draw = true
        drawState.currentIndex = drawState.currentIndex + 1
    end
end

However, the block never executes, even at start-up. Upon further inspection, print(#drawState) always results in 0 being printed to console, regardless of how many elements it contains. Any help?

  • Why do you want to count the number of non-array elements in the table? Your use case doesn't seem to need it, since you're testing the array count to see if its safe to access that index. The array part of the table is *empty*, so it can't be accessed. – Nicol Bolas Apr 27 '16 at 22:55
  • Any help? See [info](http://stackoverflow.com/tags/lua/info). – Tom Blodget Apr 27 '16 at 23:17
  • @NicolBolas Oh, thanks for the help! This seems like a really dumb mistake, but it makes obvious sense now. But to clarify... `table = { 3, 4, 5 }` creates an array that is indexable via `table[n]`, but... `table = { x = 3, y = 4, z = 5 }` is no longer indexable as an array because it is no longer considered an array? – Liam Mueller Apr 28 '16 at 03:03
  • @LiamMueller Lua tables are associative arrays. You can use a table only having numerical keys as an "ordinary or sequential array" although it's still associative under the hood. – Piglet Apr 28 '16 at 07:22
  • @Piglet It doesn't exactly goes like that. Internally, Lua tables have two parts: a hash part and an array part. The array part assumes that the table key starts in 1 and stores consecutive values. That means, the array part cannot contain nils. The hash part, which is used for tables which their keys are not integers or consecutive values, is implemented as a sparse array, so it can contains nils. And that's the reason why the # operator, which basically returns the number of consecutive values until a nil is found, cannot be applied to a table that is internally stored as a sparse array. – Diego Pino Apr 28 '16 at 10:14
  • This paper explains more "The Implementation of Lua 5.0" https://www.lua.org/doc/jucs05.pdf – Diego Pino Apr 28 '16 at 10:15
  • thx mate. guess I missinterpeted the reference. – Piglet Apr 28 '16 at 10:17
  • @DiegoPino "The array part assumes that the table key starts in 1 and stores consecutive values.": Not true. See the header comment in [table.c](http://www.lua.org/source/5.3/ltable.c.html). But, this is an implementation detail that is only of interest for performance reasons. – Tom Blodget Apr 28 '16 at 20:09

0 Answers0