Jump to content




How restrictive are arrays?


1 reply to this topic

#1 CreeperGoBoom

  • Members
  • 91 posts

Posted 15 December 2018 - 11:50 AM

OK so I know that I can do stuff like
local array = {}
array.subvar = something

How far can it continue like this?
local array = {}
array.subvar = something
array.subvar.subsubvar = something else


#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 15 December 2018 - 12:20 PM

Lua doesn't have arrays, exactly: it has tables. They're basically arrays and hashmaps mixed up together.

Your second example won't work as written, unless you declared "something" as another table somewhere first. But the basic concept is sound: if you want a table key to point to another table, that's just fine.

Note that when you attempt to assign a table to a variable, Lua actually assigns a pointer leading to that table, not the table itself. For example, if you do this:

local a = {}
b = a

... then you don't have two tables: you have two variables pointing to the same table. So, if you continue on:

a[1] = "Hello"
print(b[1])  --> Hello

This means that you can store a pointer to a table inside itself for recursive action!

a = {"Hello"}
a.a = a

print(a.a.a.a.a.a[1]) --> Hello

Functions and coroutines are assigned in the same manner.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users