lua-users home
lua-l archive

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


On May 15, 2011, at 8:51 AM, Dirk Laurie wrote:

> If t[x][y] already exists then this code has 6 lookups; if t[x], 7; 
> if t only, 8; if not even t, there is an error, as there should be.
> 
> The following code reduces 6,7,8 to 4,5,6.
> 
> function inc3(t, x, y, z)
>    local tx = t[x]
>    if not tx then tx={}; t[x] = tx end
>    local txy = tx[y]
>    if not txy then txy={}; tx[y] = txy end
>    local txyz = txy[z];
>    if not txyz then txy[z]=1 else txy[z]=txyz+1 end
> end

Missed this one when writing my response. Same as mine except the last line is more clever. Cute.

Mark