syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me

Question:
In Lua how to initialize table?

I usually rely on one of three ways to initialize a table:
dan@hp ~/ks/b/lua $
dan@hp ~/ks/b/lua $
dan@hp ~/ks/b/lua $ lua
Lua 5.3.0  Copyright (C) 1994-2015 Lua.org, PUC-Rio
>
> table1 = {}
> table1['k1'] = 'hello'
>
> table1
table: 0x20b5cb0
>
> table1.k1
hello
>
> table2 = {k2 = 'hola'}
>
> table2.k2
hola
>
> table2['k2']
hola
>
> table3 = {'gooten-tag', 'bonjour', 'ni-how'}
>
> table3[1]
gooten-tag
>
>
dan@hp ~/ks/b/lua $
dan@hp ~/ks/b/lua $
dan@hp ~/ks/b/lua $
The first two tables remind me of the behavior of Ruby-Hash objects and Python-Dictionary objects.

The last table reminds me of the behavior of Ruby-Array objects and Python-List objects.

syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me