Why do Lua arrays(tables) start at 1 instead of 0?

后端 未结 8 2152
情歌与酒
情歌与酒 2020-12-12 18:34

I don\'t understand the rationale behind the decision of this part of Lua. Why does indexing start at 1? I have read (as many others did) this great paper. It seems to me a

8条回答
  •  被撕碎了的回忆
    2020-12-12 19:11

    In your example, table[0] will always return nil(null), unless you assign value to it yourself, like table[0] = 'some value' and then table[0] will return 'some value', which you assigned.

    Here's an example:

    tbl = {"some"}
    print("tbl[0]=" .. tostring(tbl[0]))
    print("tbl[1]=" .. tostring(tbl[1]))
    nothing = {}
    print("nothing[0]=" .. tostring(nothing[0]))
    print("nothing[1]=" .. tostring(nothing[1]))
    nothing[0] = "hey"
    print("(after assign)\nnothing[0]=" .. tostring(nothing[0]))
    

提交回复
热议问题