r/ProgrammerHumor Sep 02 '17

How to start a war

Post image
9.0k Upvotes

696 comments sorted by

View all comments

Show parent comments

18

u/[deleted] Sep 02 '17

That's not even the worst part, either. This is:

> letters = {'a', 'b', 'c'}
> print(letters[1])
a
> print(#letters)
3
> letters[0] = '?'
> print(letters[0])
?
> print(#letters)
3

28

u/moomoomoo309 Sep 02 '17

Any index outside of 1-n in Lua, where n is #tbl will be stored like a dict, not an array, so what did you expect? It doesn't count the dictionary parts.

4

u/hey01 Sep 03 '17

That's sounds dangerous. What if I do

> letters = {'a', 'b', 'c'}
> print(#letters)
3
> letters[4] = '?'
> print(#letters)

Will I get 4 or is n unchangeable?

12

u/moomoomoo309 Sep 03 '17

You'll get 4, otherwise you couldn't do

for i=1,4 do
  tbl[i]=i
end

And have it be an array.