lua-users home
lua-l archive

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


So, let me see if I understand your question. I think you are trying to do something as follows:

do
  local x = 1
  function foo()
    local x = 2
    -- now, you want to access the external local variable, x
  end
end

So, perhaps you can try:

do
  local x = 1
  function foo()
    local x_up = x
    local x = 2
    -- now, you can access the x above the function through x_up
  end
end

And just curious, why do you need to write code like this?

Tai

-----Original Message-----
From: Leandro Candido [mailto:enclle@click21.com.br]
Sent: Wednesday, October 15, 2003 3:03 PM
To: Lua list
Subject: Re: local and global


Hello,

    No, what I want is to access x in [level-1], this is the x global to the
block/scope. Example:
    --x = 123 -- _G.x
    function test()
            local x = 123 --< I want to access this
            for i = 0,10 do
                local x = x + i --< [local x] = 246 + i
                if x == 130 then
                    [global x ] = x --< [x this level-1] = [local x]
                end
    end

                        Thanx,

                                                                Leandro.

----- Original Message -----
From: "Daniel Silverstone" <dsilvers@digital-scurf.org>
To: "Lua list" <lua@bazar2.conectiva.com.br>
Sent: Wednesday, October 15, 2003 6:59 AM
Subject: Re: local and global


> On Wed, 2003-10-15 at 04:22, Leandro Candido wrote:
> >     How can I access a global variable after a redeclaration as a local?
Ex:
> >             [global x] = -x -- global x = negative of local x
>
> If you *definitely* mean global rather than "local in enclosing scope"
> then...
>
> _G["x"] or just _G.x should do what you want (unless I'm horribly
> mistaken)
>
> D.
>
> --
> Daniel Silverstone                       http://www.digital-scurf.org/
> Hostmaster, Webmaster, and Chief Code Wibbler: Digital-Scurf Unlimited
> GPG Public key available from keyring.debian.org       KeyId: 20687895
>
>