4 ms·
Lua tables have two parts - the ordered part with indices 1 ... n and the other part is everything else. You can iterate over a table in various ways. pairs() i
by LogicHoleFlaw 18y ago
Lua tables have two parts - the ordered part with indices 1 ... n and the other part is everything else. You can iterate over a table in various ways. pairs() is an iterator over the entire table in an undefined order. ipairs() gives the ordered integer keys from 1 to #t (#t gives the max integer key of a table t).
My favorite aspect of Lua tables is that attribute-style access is exactly equivalent to dict-style access.
At the repl:
> tab = {} -- allocate a new table "tab"
> tab.string = "hello!"
> =tab.string == tab['string']
true
Also, you can index a Lua table by any other value. Functions, strings, tables, and booleans are all valid keys. nil is the only invalid key.
This has been your daily dose of Lua!
- silentbicycle 18y agoThanks. It looks like they nailed a big part of what I like about Python. When/if I do more projects with C, I'll check it out.