Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Lua - need more eyes

edited September 2012 in Questions Posts: 17
local ggameResourceTypes= {
        "GAMERESOURCE_COAL",
        "GAMERESOURCE_OIL",
        "GAMERESOURCE_GARBAGE",
        "GAMERESOURCE_NUCLEAR"
    }
   

    self.ggameResources= {}
    for indx,gameResourceType in ipairs(ggameResourceTypes) do
        self.ggameResources[gameResourceType]= "joe"
    end

    print(#self.ggameResources)

Why does this print 0?

Thanks

Tagged:

Comments

  • Posts: 2,161

    Because you are creating a new table indexed by strings. The # operator only works on the numerical part of a table. So:

    a = {"a","b","c"}
    

    has #a equal to 3 as the indices default to integers, but

    a = {a = "a", b = "b", c = "c"}
    

    has #a equal to 0 as the indices are strings.

    (Incidentally, to format a code block, put ~~~ either side of it, see the FAQ post for more details.)

  • Thank you! Wasted a whole hour train ride staring at it. Explains the failure of ipairs() as well.

    Thanks

Sign In or Register to comment.