[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Three dubious ways to handle deep indexing
- From: Dirk Laurie <dirk.laurie@...>
- Date: Thu, 23 Feb 2017 14:06:03 +0200
I want `a.b.c.d.e`. I'm going to test for nil, and I don't care
at what level the missing index is.
1. x = a and a.b and a.b.c and a.b.c.d and a.b.c.d.e
2. x = has(a,"b.c.d.e") with
function has(a,idx)
local j,k = idx:match"([^.]+)%.(.*)"
if not k then return a[idx]
else return has(a[j],k)
end
end
3, debug.setmetatable(nil,{__index=function() return nil end})
x = a.b.c.d.e