Deceptively basic Lua tables question

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Deceptively basic Lua tables question

Post 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"?
Last edited by Taehl on Thu Nov 18, 2010 2:44 am, edited 1 time in total.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
Mud
Citizen
Posts: 98
Joined: Fri Nov 05, 2010 4:54 am

Re: Deceptively basic Lua tables question

Post 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. :)
Last edited by Mud on Thu Nov 18, 2010 3:08 am, edited 1 time in total.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Deceptively basic Lua tables question

Post by Taehl »

Whoops, yeah, that's that I mean.

Anyway, thank you, Mud, that's exactly what I needed.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Deceptively basic Lua tables question

Post 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?
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
Mud
Citizen
Posts: 98
Joined: Fri Nov 05, 2010 4:54 am

Re: Deceptively basic Lua tables question

Post 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.
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: Deceptively basic Lua tables question

Post 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:
Hello, I am not dead.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Deceptively basic Lua tables question

Post 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.
When I write def I mean function.
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: Deceptively basic Lua tables question

Post 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.
Hello, I am not dead.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Deceptively basic Lua tables question

Post by bartbes »

Yes it's even harder to spot typos, yay! Oh, wait...
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Deceptively basic Lua tables question

Post by kikito »

Well, to each one his thing. Some people don't like Lua just because it isn't strongly typed :crazy: .
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests