[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Three dubious ways to handle deep indexing
- From: Martin <eden_martin_fuhrspam@...>
- Date: Thu, 23 Feb 2017 15:40:26 -0800
On 02/23/2017 04:06 AM, Dirk Laurie wrote:
> 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
This was my first issue in Lua that annoyed me. Traditional solution
(1.) with infinite "and a.b and a.b.c" hurts my feelings and fingers.
Finally I came to something like solution (2.) (but w/o recursion and
with names list in vararg "...").
Solution (3.) looks neat. Will try it.
-- Martin