Deceptively basic Lua tables question
- Taehl
- Dreaming in associative arrays
- Posts: 1025
- Joined: Mon Jan 11, 2010 5:07 am
- Location: CA, USA
- Contact:
Deceptively basic Lua tables question
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+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Re: Deceptively basic Lua tables question
Code: Select all
if not something[n] then
something[n]={}
end
something[n]=x
Code: Select all
if not something[n] then
something[n]={}
end
something[n][x] = y
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'
Code: Select all
something = {
foo = {
bar = {
zip = {
zap = "donut"
}
}
}
}
Last edited by Mud on Thu Nov 18, 2010 3:08 am, edited 1 time in total.
- 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
Whoops, yeah, that's that I mean.
Anyway, thank you, Mud, that's exactly what I needed.
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+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
- 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
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?
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+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Re: Deceptively basic Lua tables question
Only if you index the table without assigning anything to it, which would be pointless on a table modified this way. For instance:Taehl wrote:I just realized something: That could very quickly make me end up with lots of empty tables.
Code: Select all
// check if something has been assigned to something.foo.bar
if not something.foo.bar then
...
Unreferenced tables are automatically collected. Unused tables aren't.Taehl wrote:Is there any way to clear unused tables? Some kind of garbage collection?
Yup.Also, will tables with this metatable trick work properly with things like pairs and ipairs?
Re: Deceptively basic Lua tables question
I once crashed roblox using ipairs and a table that never returns nil...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.
Hello, I am not dead.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Deceptively basic Lua tables question
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.
Re: Deceptively basic Lua tables question
Wow.. That language sounds AWESOME.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.
Hello, I am not dead.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Deceptively basic Lua tables question
Yes it's even harder to spot typos, yay! Oh, wait...
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Deceptively basic Lua tables question
Well, to each one his thing. Some people don't like Lua just because it isn't strongly typed .
When I write def I mean function.
Who is online
Users browsing this forum: Amazon [Bot] and 9 guests