lua-users home
lua-l archive

Three dubious ways to handle deep indexing

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


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

AltStyle によって変換されたページ (->オリジナル) /