Page 1 of 2

Deceptively basic Lua tables question

Posted: Thu Nov 18, 2010 1:28 am
by Taehl
In Love, I find myself doing things like "if not something[n] then something[n]={} end something[n][x] = y" a /lot/. I imagine there's a better way to say "if a table doesn't have a given key, assume its value is an empty table"?

Re: Deceptively basic Lua tables question

Posted: Thu Nov 18, 2010 2:29 am
by Mud

Code: Select all

if not something[n] then 
   something[n]={} 
end 
something[n]=x
That code doesn't make a lot of sense, given that you're nuking the table you just created. Did you mean something like this?

Code: Select all

if not something[n] then 
   something[n]={} 
end
something[n][x] = y
What would be pretty common for an N-dimensional sparse array. If that's what you have in mind (i.e. when you attempt to index a field as if it were a table, for it automatically become a table), you can do that with a metatable:

Code: Select all

proxy = {}
function proxy.__index(t,k)
   local auto = setmetatable({}, proxy)
   rawset(t,k,auto)
   return auto
end

local something = setmetatable({}, proxy)

something.foo.bar.zip.zap = 'donut'
'something' now looks like this:

Code: Select all

something = {
  foo =  {
    bar =  {
      zip =  {
        zap = "donut"
      }
    }
  }
}
Of course this means no index attempt will ever return nil, so code that relies on that would fail, and you'd need to be extra careful about typos. :)

Re: Deceptively basic Lua tables question

Posted: Thu Nov 18, 2010 2:43 am
by Taehl
Whoops, yeah, that's that I mean.

Anyway, thank you, Mud, that's exactly what I needed.

Re: Deceptively basic Lua tables question

Posted: Thu Nov 18, 2010 3:30 am
by Taehl
I just realized something: That could very quickly make me end up with lots of empty tables. Is there any way to clear unused tables? Some kind of garbage collection?

Also, will tables with this metatable trick work properly with things like pairs and ipairs?

Re: Deceptively basic Lua tables question

Posted: Thu Nov 18, 2010 3:55 am
by Mud
Taehl wrote:I just realized something: That could very quickly make me end up with lots of empty tables.
Only if you index the table without assigning anything to it, which would be pointless on a table modified this way. For instance:

Code: Select all

// check if something has been assigned to something.foo.bar
if not something.foo.bar then
  ...
Will always be true for our special table (assuming we're checking for nil and not false), so it's a meaningless test, and that's the only way you're going to get a bunch of unused tables. If you need to use the table like that, you probably don't want to use that metatable trick.
Taehl wrote:Is there any way to clear unused tables? Some kind of garbage collection?
Unreferenced tables are automatically collected. Unused tables aren't.
Also, will tables with this metatable trick work properly with things like pairs and ipairs?
Yup.

Re: Deceptively basic Lua tables question

Posted: Thu Nov 18, 2010 12:30 pm
by zac352
Mud wrote: Of course this means no index attempt will ever return nil, so code that relies on that would fail, and you'd need to be extra careful about typos. :)
I once crashed roblox using ipairs and a table that never returns nil... :neko:

Re: Deceptively basic Lua tables question

Posted: Thu Nov 18, 2010 12:53 pm
by kikito
A nice solution would be having nil[whatever] return nil silently, without raising an error. I believe in Squirrel they do just that. I don't think it can be done in Lua.

Re: Deceptively basic Lua tables question

Posted: Thu Nov 18, 2010 1:28 pm
by zac352
kikito wrote:A nice solution would be having nil[whatever] return nil silently, without raising an error. I believe in Squirrel they do just that. I don't think it can be done in Lua.
Wow.. That language sounds AWESOME.

Re: Deceptively basic Lua tables question

Posted: Thu Nov 18, 2010 2:14 pm
by bartbes
Yes it's even harder to spot typos, yay! Oh, wait...

Re: Deceptively basic Lua tables question

Posted: Thu Nov 18, 2010 2:34 pm
by kikito
Well, to each one his thing. Some people don't like Lua just because it isn't strongly typed :crazy: .