lua-users home
lua-l archive

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


>
> I wrote a string interpolator a while ago, but the biggest problem I
> found was locals and upvalues. There's no good way to look them up by
> name.
>
> Or I can just keep dreaming that someday Lua will provide a method to
> look up variables in the current local scope by name... :~)
>

function locals()
    local t={}
    for i=1,20 do
        k,v=debug.getlocal(2,i)
        if k==nil then break else t[k]=v end
    end
    return t
end

do local a=42; s=locals() end
print (s.a)
    42